草庐IT

Co-variant

全部标签

c++ - 如何迭代 std::variant 的类型?

我有一些变体usingV=std::variant和一个带有原型(prototype)的函数Vparse(constjson&).该函数应该尝试解析所有类型(例如A、B,然后是C)直到第一次成功(它应该隐式地解析,因为及时会有很多类型)。如何实现这种东西?我们可以使用std::variant_size不知何故。Here是接近我需要的东西。我的解决方案是明确列出所有类型的解析器。Vparse(constjson&i_j){usingParser=std::function;staticconstautops=std::vector{[](constauto&j)->MaybeV{retu

c++ - boost::variant - 为什么模板参数的优先级高于 const 字符串参数

我在以下代码中看到一个我不理解的行为。关键是,如果我声明operator()的第二个重载,如下所示:booloperator()(Tother)constbooloperator()(constT&other)const程序的输出是:string但是如果我使用下面的声明:booloperator()(T&other)const输出将是:othertype有人可以解释一下为什么在后一种情况下没有调用operator()(conststring&other)吗?#include"boost/variant/variant.hpp"#include"boost/variant/apply_v

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++ - co_await 似乎不是最优的?

我有一个异步函数voidasync_foo(A&a,B&b,C&c,functioncallback);我想在无堆栈协程中使用它,所以我写autocoro_foo(A&a,B&b,C&c,X&x)/*->Y*/{structAwaitable{boolawait_ready()constnoexcept{returnfalse;}boolawait_suspend(coroutine_handleh){async_foo(*a_,*b_,*c_,[this,h](X&x,Y&y){*x_=std::move(x);y_=std::move(y);h.resume();});}Yawai

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++17 通过生成预先声明的类型列表的笛卡尔积来制作 std::variant

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

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++ - 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++ - 一般从 boost::variant<T> 转换为类型

我有一个typedefboost::variantvariant我用它来在结构中存储不同类型的值。只有一个特定类型会存储在该结构中,但是我有这些结构的vector,我需要通过它并从变体中获取实际类型。现在,当我需要将类型从这个变体中转换出来时,我会这样做:variantsecond=mystruct.variant;if(second.which()==5)//string{std::stringval=boost::get(second);modvalue->AddNodeAttribute(key,val);}elseif(second.which()==0)//int{intva

c++ - 如何构建指向 VARIANT 的指针的 SAFEARRAY?

我正在尝试通过以下方法使用COM组件:HRESULT_stdcallRun([in]SAFEARRAY(BSTR)paramNames,[in]SAFEARRAY(VARIANT*)paramValues);如何在C/C++中创建paramValues数组? 最佳答案 添加到上面的答案以供future读者引用:在IDL中,SAFEARRAY(...)表示指向数组描述符的指针。但在C++中,SAFEARRAY表示数组描述符。所以IDL的SAFEARRAY(...)真的是C++的SAFEARRAY*.这让我困惑不已。为了让事情变得更有趣