boost tuple:增加元素的最大数量

发布于 2024-11-16 05:49:20 字数 398 浏览 3 评论 0原文

boost 元组文档说:

当前版本支持元组 具有 0-10 个元素。如有必要,可将 上限可以增加到, 比如说,几十个元素。

但是,我找不到它在哪里说明如何执行此操作。

我希望元组具有 BOOST_MPL_LIMIT_VECTOR_SIZE 元素(默认为 20)。这是因为我在 mpl::vectorsboost::tuples 之间进行映射,并且希望所有容器具有相同数量的元素。

The boost tuple documentation says:

The current version supports tuples
with 0-10 elements. If necessary, the
upper limit can be increased up to,
say, a few dozen elements.

However, I could not find where it says how to do this.

I would like the tuple to have BOOST_MPL_LIMIT_VECTOR_SIZE elements (20 by default). This is because I am mapping between mpl::vectors and boost::tuples and would like all the containers to have the same number of elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-11-23 05:49:20

元组类是通过以下方式声明的:

// - tuple forward declaration -----------------------------------------------
template <
  class T0 = null_type, class T1 = null_type, class T2 = null_type,
  class T3 = null_type, class T4 = null_type, class T5 = null_type,
  class T6 = null_type, class T7 = null_type, class T8 = null_type,
  class T9 = null_type>
class tuple;

因此,它的模板参数计数设置为上限 10。但是,将来(C++0x)可以这样声明它:

template<class... Params> class tuple;

所以,我不知道认为目前实践中还不可能提高上限。这可以通过以下方式实现:

#define VARIADIC_PARAMS_DEFVAL0(type_name, value)
#define VARIADIC_PARAMS_DEFVAL1(type_name, value)   type_name##0 = value
#define VARIADIC_PARAMS_DEFVAL2(type_name, value)   VARIADIC_PARAMS_DEFVAL1(type_name, value), type_name##1 = value
#define VARIADIC_PARAMS_DEFVAL3(type_name, value)   VARIADIC_PARAMS_DEFVAL2(type_name, value), type_name##2 = value
#define VARIADIC_PARAMS_DEFVAL4(type_name, value)   VARIADIC_PARAMS_DEFVAL3(type_name, value), type_name##3 = value
#define VARIADIC_PARAMS_DEFVAL5(type_name, value)   VARIADIC_PARAMS_DEFVAL4(type_name, value), type_name##4 = value
#define VARIADIC_PARAMS_DEFVAL6(type_name, value)   VARIADIC_PARAMS_DEFVAL5(type_name, value), type_name##5 = value
#define VARIADIC_PARAMS_DEFVAL7(type_name, value)   VARIADIC_PARAMS_DEFVAL6(type_name, value), type_name##6 = value
#define VARIADIC_PARAMS_DEFVAL8(type_name, value)   VARIADIC_PARAMS_DEFVAL7(type_name, value), type_name##7 = value
#define VARIADIC_PARAMS_DEFVAL9(type_name, value)   VARIADIC_PARAMS_DEFVAL8(type_name, value), type_name##8 = value
#define VARIADIC_PARAMS_DEFVAL10(type_name, value)  VARIADIC_PARAMS_DEFVAL9(type_name, value), type_name##9 = value
// ...
#define VARIADIC_PARAMS_DEFVAL100(type_name, value) VARIADIC_PARAMS_DEFVAL99(type_name, value), type_name##99 = value
#define VARIADIC_PARAMS_DEFVAL(type_name, value, n) VARIADIC_PARAMS_DEFVAL##n(type_name, value)

#define VARIADIC_MACRO_INVOKE0(macro)    macro(0)
#define VARIADIC_MACRO_INVOKE1(macro)    VARIADIC_MACRO_INVOKE0(macro); macro(1)
#define VARIADIC_MACRO_INVOKE2(macro)    VARIADIC_MACRO_INVOKE1(macro); macro(2)
#define VARIADIC_MACRO_INVOKE3(macro)    VARIADIC_MACRO_INVOKE2(macro); macro(3)
#define VARIADIC_MACRO_INVOKE4(macro)    VARIADIC_MACRO_INVOKE3(macro); macro(4)
#define VARIADIC_MACRO_INVOKE5(macro)    VARIADIC_MACRO_INVOKE4(macro); macro(5)
#define VARIADIC_MACRO_INVOKE6(macro)    VARIADIC_MACRO_INVOKE5(macro); macro(6)
#define VARIADIC_MACRO_INVOKE7(macro)    VARIADIC_MACRO_INVOKE6(macro); macro(7)
#define VARIADIC_MACRO_INVOKE8(macro)    VARIADIC_MACRO_INVOKE7(macro); macro(8)
#define VARIADIC_MACRO_INVOKE9(macro)    VARIADIC_MACRO_INVOKE8(macro); macro(9)
#define VARIADIC_MACRO_INVOKE10(macro)   VARIADIC_MACRO_INVOKE9(macro); macro(10)
// ...
#define VARIADIC_MACRO_INVOKE100(macro)  VARIADIC_MACRO_INVOKE99(macro); macro(100)
#define VARIADIC_MACRO_INVOKE(macro, n)  VARIADIC_MACRO_INVOKE##n(macro)

#define TUPPLE_UPPER_LIMIT      50

#define FORWARD_DECLARE_TUPPLE(N)                       \
template<VARIADIC_PARAMS_DEFVAL(class, null_type, N)    \
class tuple;

FS_VARIADIC_MACRO_INVOKE(FORWARD_DECLARE_TUPPLE, TUPPLE_UPPER_LIMIT);

The tuple class is declared in the following way:

// - tuple forward declaration -----------------------------------------------
template <
  class T0 = null_type, class T1 = null_type, class T2 = null_type,
  class T3 = null_type, class T4 = null_type, class T5 = null_type,
  class T6 = null_type, class T7 = null_type, class T8 = null_type,
  class T9 = null_type>
class tuple;

Thus, its template parameter count is set to a upper limit of 10. However, it would be possible in the future (C++0x) to declare it like that:

template<class... Params> class tuple;

So, I don't think it's currently possible to increase the upper limit in practice. It would been possible in the following way:

#define VARIADIC_PARAMS_DEFVAL0(type_name, value)
#define VARIADIC_PARAMS_DEFVAL1(type_name, value)   type_name##0 = value
#define VARIADIC_PARAMS_DEFVAL2(type_name, value)   VARIADIC_PARAMS_DEFVAL1(type_name, value), type_name##1 = value
#define VARIADIC_PARAMS_DEFVAL3(type_name, value)   VARIADIC_PARAMS_DEFVAL2(type_name, value), type_name##2 = value
#define VARIADIC_PARAMS_DEFVAL4(type_name, value)   VARIADIC_PARAMS_DEFVAL3(type_name, value), type_name##3 = value
#define VARIADIC_PARAMS_DEFVAL5(type_name, value)   VARIADIC_PARAMS_DEFVAL4(type_name, value), type_name##4 = value
#define VARIADIC_PARAMS_DEFVAL6(type_name, value)   VARIADIC_PARAMS_DEFVAL5(type_name, value), type_name##5 = value
#define VARIADIC_PARAMS_DEFVAL7(type_name, value)   VARIADIC_PARAMS_DEFVAL6(type_name, value), type_name##6 = value
#define VARIADIC_PARAMS_DEFVAL8(type_name, value)   VARIADIC_PARAMS_DEFVAL7(type_name, value), type_name##7 = value
#define VARIADIC_PARAMS_DEFVAL9(type_name, value)   VARIADIC_PARAMS_DEFVAL8(type_name, value), type_name##8 = value
#define VARIADIC_PARAMS_DEFVAL10(type_name, value)  VARIADIC_PARAMS_DEFVAL9(type_name, value), type_name##9 = value
// ...
#define VARIADIC_PARAMS_DEFVAL100(type_name, value) VARIADIC_PARAMS_DEFVAL99(type_name, value), type_name##99 = value
#define VARIADIC_PARAMS_DEFVAL(type_name, value, n) VARIADIC_PARAMS_DEFVAL##n(type_name, value)

#define VARIADIC_MACRO_INVOKE0(macro)    macro(0)
#define VARIADIC_MACRO_INVOKE1(macro)    VARIADIC_MACRO_INVOKE0(macro); macro(1)
#define VARIADIC_MACRO_INVOKE2(macro)    VARIADIC_MACRO_INVOKE1(macro); macro(2)
#define VARIADIC_MACRO_INVOKE3(macro)    VARIADIC_MACRO_INVOKE2(macro); macro(3)
#define VARIADIC_MACRO_INVOKE4(macro)    VARIADIC_MACRO_INVOKE3(macro); macro(4)
#define VARIADIC_MACRO_INVOKE5(macro)    VARIADIC_MACRO_INVOKE4(macro); macro(5)
#define VARIADIC_MACRO_INVOKE6(macro)    VARIADIC_MACRO_INVOKE5(macro); macro(6)
#define VARIADIC_MACRO_INVOKE7(macro)    VARIADIC_MACRO_INVOKE6(macro); macro(7)
#define VARIADIC_MACRO_INVOKE8(macro)    VARIADIC_MACRO_INVOKE7(macro); macro(8)
#define VARIADIC_MACRO_INVOKE9(macro)    VARIADIC_MACRO_INVOKE8(macro); macro(9)
#define VARIADIC_MACRO_INVOKE10(macro)   VARIADIC_MACRO_INVOKE9(macro); macro(10)
// ...
#define VARIADIC_MACRO_INVOKE100(macro)  VARIADIC_MACRO_INVOKE99(macro); macro(100)
#define VARIADIC_MACRO_INVOKE(macro, n)  VARIADIC_MACRO_INVOKE##n(macro)

#define TUPPLE_UPPER_LIMIT      50

#define FORWARD_DECLARE_TUPPLE(N)                       \
template<VARIADIC_PARAMS_DEFVAL(class, null_type, N)    \
class tuple;

FS_VARIADIC_MACRO_INVOKE(FORWARD_DECLARE_TUPPLE, TUPPLE_UPPER_LIMIT);
瀞厅☆埖开 2024-11-23 05:49:20

好消息。找到答案,就是用宏重新定义max参数即可。 boost中的FUSION库重新定义了元组。按照以下步骤,您可以

  1. 在包含任何元组文件之前轻松扩展元组参数定义FUSION_MAX_VECTOR_SIZE
  2. 包含融合或TR1版本元组头文件而不是普通元组头

    <前><代码> #define FUSION_MAX_VECTOR_SIZE 50
    #include;

为了更好地理解上面的代码,可以参考

文件中的头文件“boost/tr1/tuple.hpp”,它有另一个“BOOST_TR1_USE_OLD_TUPLE”来引用旧的元组实现。

在融合的元组实现“boost/fusion/tuple/tuple.hpp”中,还有另一个宏。 “BOOST_FUSION_DONT_USE_PREPROCESSED_FILES”。如果没有定义,库将使用预先创建的头文件,最大参数为50。如果您需要更多,我相信您可以将此宏定义为true。从代码来看,虽然我还没有真正尝试过,但参数多一点应该没问题。因为 50 对我来说已经足够了;)

如果您只定义 FUSION_MAX_VECTOR_SIZE 并且您需要的参数超过 50,则会发现另一个问题。您必须为矢量模板提供自己的头文件,而不是使用现有的进程头文件。除了以下代码之外,您还需要定义宏“BOOST_FUSION_DONT_USE_PREPROCESSED_FILES”以排除经过处理的头文件

#if (FUSION_MAX_VECTOR_SIZE > 50)

#include <boost/fusion/container/vector/vector50.hpp>

namespace boost 
{ 
namespace mpl 
{
#define BOOST_PP_ITERATION_PARAMS_1 (3,(51, FUSION_MAX_VECTOR_SIZE, <boost/mpl/vector/aux_/numbered.hpp>))
#include BOOST_PP_ITERATE()
}

namespace fusion
{
    struct vector_tag;
    struct fusion_sequence_tag;
    struct random_access_traversal_tag;

    // expand vector51 to max
    #define BOOST_PP_FILENAME_1 <boost/fusion/container/vector/detail/vector_n.hpp>
    #define BOOST_PP_ITERATION_LIMITS (51, FUSION_MAX_VECTOR_SIZE)
    #include BOOST_PP_ITERATE()
}
}

Good news. Find the answer, it is just use the macro to re-define the max parameters. The library FUSION in boost re-define the tuple. Follow the following steps, you can easily extend the tuple parameters

  1. define the FUSION_MAX_VECTOR_SIZE before including any tuple file
  2. include the fusion or TR1 version tuple header file instead of the normal tuple header

     #define FUSION_MAX_VECTOR_SIZE 50
     #include <boost/tr1/tuple.hpp>
    

To understand the above code better, you can refer to the header file "boost/tr1/tuple.hpp"

in the file, it has another "BOOST_TR1_USE_OLD_TUPLE" to refer to the old tuple implementation.

in the fusion's tuple implementation "boost/fusion/tuple/tuple.hpp", there is another macro. "BOOST_FUSION_DONT_USE_PREPROCESSED_FILES". if it is not defined, the library will use the pre-created header file, the maximum parameter are 50. if you need more, I believe you can just define this macro to true. From the code, it should be OK to have more parameters although I haven't really try it out. because 50 to me is far more enough ;)

find another issue if you just define FUSION_MAX_VECTOR_SIZE and you need the parameters are more than 50. you have to come out your own header file for the vector template instead of using the existing process header file. besides the following code, you also need define the macro "BOOST_FUSION_DONT_USE_PREPROCESSED_FILES" to exclude the proprocessed header file

#if (FUSION_MAX_VECTOR_SIZE > 50)

#include <boost/fusion/container/vector/vector50.hpp>

namespace boost 
{ 
namespace mpl 
{
#define BOOST_PP_ITERATION_PARAMS_1 (3,(51, FUSION_MAX_VECTOR_SIZE, <boost/mpl/vector/aux_/numbered.hpp>))
#include BOOST_PP_ITERATE()
}

namespace fusion
{
    struct vector_tag;
    struct fusion_sequence_tag;
    struct random_access_traversal_tag;

    // expand vector51 to max
    #define BOOST_PP_FILENAME_1 <boost/fusion/container/vector/detail/vector_n.hpp>
    #define BOOST_PP_ITERATION_LIMITS (51, FUSION_MAX_VECTOR_SIZE)
    #include BOOST_PP_ITERATE()
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文