gpt4 book ai didi

c++ - 如何增加 boost::variant 可以处理的类型数量

转载 作者:可可西里 更新时间:2023-11-01 16:01:14 27 4
gpt4 key购买 nike

我正在为 verilog 语言设计一个解析器,其中一个规则有 25 个组件,我需要一个大的 boost::variant 来保存它:

typedef boost::variant<
shared_ptr<T_module_item__port_declaration>
, shared_ptr<T_module_item__generate_region>
, shared_ptr<T_module_item__specify_block>
, shared_ptr<T_module_item__parameter_declaration>
, shared_ptr<T_module_item__specparam_declaration>
, shared_ptr<T_module_item__net_declaration>
, shared_ptr<T_module_item__reg_declaration>
, shared_ptr<T_module_item__integer_declaration>
, shared_ptr<T_module_item__real_declaration>
, shared_ptr<T_module_item__time_declaration>
, shared_ptr<T_module_item__realtime_declaration>
, shared_ptr<T_module_item__event_declaration>
, shared_ptr<T_module_item__genvar_declaration>
, shared_ptr<T_module_item__task_declaration>
, shared_ptr<T_module_item__function_declaration>
, shared_ptr<T_module_item__local_parameter_declaration>
, shared_ptr<T_module_item__parameter_override>
, shared_ptr<T_module_item__continuous_assign>
, shared_ptr<T_module_item__gate_instantiation>
, shared_ptr<T_module_item__udp_instantiation>
, shared_ptr<T_module_item__module_instantiation>
, shared_ptr<T_module_item__initial_construct>
, shared_ptr<T_module_item__always_construct>
, shared_ptr<T_module_item__loop_generate_construct>
, shared_ptr<T_module_item__conditional_generate_construct>
> module_item ;

但是 g++ 提示 boost::variant 只能容纳不超过 20 种类型。

verilogast.h|1129 col 2| error: wrong number of template arguments (25, should be 20)
|| > module_item ;
|| ^
/usr/include/boost/variant/variant_fwd.hpp|213 col 53| error: provided for ‘template<class T0, class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15, class T16, class T17, class T18, class T19> class boost::variant’
|| template < BOOST_VARIANT_AUX_DECLARE_PARAMS > class variant;

我尝试将 BOOST_VARIANT_LIMIT_TYPES 重新定义为更大的值:

#define BOOST_VARIANT_LIMIT_TYPES 30
#include<boost/variant.hpp>

但是错误依旧,

最佳答案

clang++ 和 g++ 在 c++98 模式下的错误(你似乎得到的)非常短(遗憾的是没用)。在 c++11 中,错误更大并揭示了关键问题:

error: too many template arguments for class template 'list'
typedef typename mpl::list< T... >::type type;

如果您查看 Boost.MPL documentation你可以看到你需要添加:

#define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
#define BOOST_MPL_LIMIT_LIST_SIZE 30

默认情况下,您只能使列表的大小为 30、40 或 50,如果您想要更多,则必须生成自定义 header 。

Running on Coliru

关于c++ - 如何增加 boost::variant 可以处理的类型数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com