草庐IT

c++ - 在 g++ 中使用 std::variant

如何在g++中使用std::variant?为什么std::experimental中没有std::variant(虽然std::optional是)?我需要什么版本的g++?我不想使用boost,我只想使用标准库。编辑:似乎只有g++7支持std::variant。那么我的问题是:什么时候发布,使用它的实验版会遇到什么问题? 最佳答案 Thispage说,GCC7有std::variant。 关于c++-在g++中使用std::variant,我们在StackOverflow上找到一个

c++ - Boost变体失败

我有这样的代码:boost::variantprmJointDef;b2DistanceJointDef&prmDistaceJointDef=boost::get(prmJointDef);错误是:source\Scene\Components\JointComponent.cpp:51:96:error:nomatchingfunctionforcallto'get(boost::variant&)'\source\Scene\Components\JointComponent.cpp:51:96:note:candidatesare:boost/optional/optional

c++ - 使用包含不完整类型的 `boost::variant` 递归定义和访问 `std::vector` - libstdc++ 与 libc++

我正在尝试定义和访问“递归”boost::variant使用incomplete包装类和std::vector作为我的间接技巧。我的实现适用于libstdc++,但不适用于libc++。这是我定义变体的方式:structmy_variant_wrapper;usingmy_variant_array=std::vector;//;structmy_variant_wrapper{my_variant_v;templatemy_variant_wrapper(Ts&&...xs):_v(std::forward(xs)...){}};我正在使用std::vector引入间接(以便动态分配

C++ 相互递归变体类型(再次)

我遇到的问题类似于此处描述的问题:C++MutuallyRecursiveVariantType我正在尝试用C++创建一个JSON表示。许多库已经提供了非常快的优秀JSON表示和解析器,但我并没有重新发明这个轮子。我需要创建一个支持特定条件下某些空间优化的C++JSON表示。简而言之,当且仅当JSON数组包含同质数据时,而不是将每个元素存储为臃肿的变体类型,我需要原生类型的紧凑存储。我还需要支持异构数组和标准嵌套JSON对象。以下是代码的“如果愿望是马,乞丐会骑”的版本,旨在清楚地说明意图,但显然是错误的,因为在任何声明存在之前就使用了类型。我想避免在类型中多次指定相同的信息(即数组、

c++17 通过生成预先声明的类型列表的笛卡尔积来制作 std::variant

假设我有一个包含三个模板类型参数的类。templatestructConfiguredPipeline{};并且有以下类稍后在实例化ConfiguredPipeline时使用:templatestructCriteriaList{};usingSupportedCriteria=CriteriaList;templatestructStrategiesList{};usingSupportedStrategies=StrategiesList;templatestructTransformerList{};usingSupportedTransformer=TransformerLis

c++ - Clang 声称通用 lambda 参数的 constexpr 成员不是 constexpr

我想编写一个通用的lambda作为变体的访问者。这个变体的成员包含一个constexpr成员值,我想在访问者中使用它。例如:#includetemplatestructS{constexprstaticintthis_r=r;};intf(std::variant,S,S>v){returnstd::visit([](autoconst&arg){ifconstexpr(arg.this_r==0){return42;}else{returnarg.this_r;}},v);}intg(){std::variant,S,S>x=S();returnf(x);}GCC乐于从大约versi

c++ - 如何打印可流类型的 boost::variant?

我觉得我有一个严重的“Doh!”此刻……我目前正在尝试实现:std::ostream&operatorMyType包含boost::int、char和bool的变体。IE:使我的变体可流式传输。我试过这样做:outMyTypePrintVisitor有一个模板函数,它使用boost::lexical_cast将int、char或bool转换为字符串。但是,这不会编译,错误是apply_visitor不是MyType的函数。然后我这样做了:if(type.variant.type()==int)out(type.variant);//Soonforcharandbool...我是否缺少更

c++ - 在 C++11 中构建 union 元组

元组是一种likestructs.是否也有表现得像union的元组?或者我可以在元组中访问成员的union,例如my_union_tupleu;get(u);get(u);//C++14only,orseebelow对于第二行,请参阅here.当然,该解决方案不仅适用于特定的union,例如,但对于任意类型和类型数量。 最佳答案 没有std::tuple表示A和B。如果您想要一个类型安全的类union容器,请查看boostvariant.boost::variantv;v="hello";std::cout它确实为游客提供了安全的交

c++ - boost::variant 和函数重载决议

以下代码无法编译:#includeclassA{};classB{};classC{};classD{};usingv1=boost::variant;usingv2=boost::variant;intf(v1const&){return0;}intf(v2const&){return1;}intmain(){returnf(A{});}gcc和clang都提示:test1.cpp:Infunction‘intmain()’:test1.cpp:18:17:error:callofoverloaded‘f(A)’isambiguousreturnf(A{});^test1.cpp:1

c++ - 如何检查模板类型是否是变体类型的类型之一?

考虑变体类型和模板函数,如何检查模板类型是变体类型之一?有没有比下面的方法更优雅的方法?typedefboost::variantVar;templatevoidf(constT&x){BOOST_STATIC_ASSERT(boost::is_same::value||boost::is_same::value);}注意:我使用Boost1.57和gcc4.8.3。我不使用C++11是为了与旧的gcc版本兼容。 最佳答案 使用MPL:#include#includetypedefboost::variantVar;template