草庐IT

c++ - C++ 标准库中 boost::variant 的等价物是什么?

我正在寻找C风格union的替代方案。boost::variant就是这样一种选择。标准C++中有什么吗?union{inti;doubled;} 最佳答案 正如几位评论者所说:不,标准C++中没有类似BoostVariant的。也许几年后会有,但为什么要等一下——今天就使用BoostVariant!编辑(四年后,2016年):在C++17中将有std::variant。与boost::variant类似但不相同。所以当你的编译器支持C++17的时候,你在标准库中就有了解决方案。 关于c

c++ - 实现类 std::variant 类时存储类型标记的问题

我的目标是写std::variant,可能还没有完全成熟,但至少有完整的构造函数/析构函数对和std::get()功能。我尝试使用char数组保留内存。它的大小由最大的类型决定,使用find_biggest_size()找到。功能。构造函数使用静态断言,因为它执行检查类型是否在指定类型列表中。目前,构造函数和就地构造函数都可以工作。templateclassvariant{charobject[find_biggest_size::value];public:templatevariant(T&&other){static_assert(is_present::value,"typei

c++ - 具有 std::map 和 std::variant 的不完整类型

考虑在std::variant之上的递归变体的这种简化且非常具体的实现:#include#includestructrecursive_tag;templatestructRecursiveVariant;templatestructRecursiveVariant>:std::variant>>>{usingunderlying=std::variant>>>;usingunderlying::underlying;};intmain(){RecursiveVariant>rv;}由于试图实例化std::pair,因此无法在gcc7/8上编译,它本身就失败了,因为recursive_

c++ - 无法使用各种 lambda 表达式初始化 std::variant

我在玩std::variant,lambdas和std::future,当我尝试将它们组合在一起时得到了super奇怪的结果。以下是示例:usingvariant_t=std::variant(int)>,std::function>;autof1=[](int){returnstd::async([]{return1;});};autof2=[](int){returnstd::async([]{});};variant_tv1(std::move(f1));//!!!whyDOESthisonecompilewhenitSHOULDN'T?autoidx1=v1.index();/

c++ - std::variant 和 boost::variant 有什么区别?

在answer中对于这个SO问题:Whatistheequivalentofboost::variantintheC++standardlibrary?提到boost::variant和std::variant有些不同。就使用这些类的人而言,有什么区别?委员会表示采用具有这些差异的std::variant的动机是什么?在使用其中任何一种编码时我应该注意什么,以保持与切换到另一种的最大兼容性?(动机是在C++17之前的代码中使用boost::variant) 最佳答案 分配/就位行为:boost::variant可能allocatem

c++ - 在从 std::variant 继承的类上使用 std::visit - libstdc++ vs libc++

考虑以下代码片段:structv:std::variant>{};intmain(){std::visit([](auto){},v{0});}clang++7with-stdlib=libc++-std=c++2a编译代码;g++9with-std=c++2a无法编译代码,出现以下错误:/opt/compiler-explorer/gcc-trunk-20180711/include/c++/9.0.0/variant:94:29:error:incompletetype'std::variant_size'usedinnestednamespecifierinlineconstex

c++ - 如何检查 std::variant 是否可以保存某种类型

我有一个包含std::variant的类。这个std::variant类型只允许保存一个特定的类型列表。我有模板函数,它允许类的用户将各种值插入到std::unordered_map中,映射包含这种变体类型的值。即,仅当其类型在特定类型列表中时,才允许用户插入值。但是,我不希望用户自己定义这个类型列表。classGLCapabilities{public:usingVariantType=std::variant;//infuturethiswouldhaveothertypestemplatestd::enable_if_tAddCapability(constGLenumparam

c++ - 检查 boost::variant<T> 是否为空

我的程序中有一个boost::variant,我想检查变体本身是否已初始化,以及其中一种类型中是否包含值。我已经在变体上尝试了empty(),但这似乎不起作用。也不会检查NULL。有人知道如何检查吗?编辑:好的,它似乎永远不会为空,但它所包含的类型中并不总是有值,那么如何检查无值情况? 最佳答案 如果您看到myquestionregardingneveremptyguaranteeandsinglestorage,boost::variant确实支持名为boost::blank的类似NIL的值类型.这将保证变体永远不会使用堆作为备份

c++ - boost::variant - 为什么 "const char*"转换为 "bool"?

我声明了一个boost::variant,它接受三种类型:string、bool和int。以下代码显示我的变体接受constchar*并将其转换为bool。boost::variant接受和转换不在其列表中的类型是正常行为吗?#include#include"boost/variant/variant.hpp"#include"boost/variant/apply_visitor.hpp"usingnamespacestd;usingnamespaceboost;typedefvariantMyVariant;classTestVariant:publicboost::static_

c++ - 为什么 std::get for variant 会引发失败而不是未定义的行为?

根据cppreference如果variant中包含的类型不是预期的,则variant的std::get会抛出std::bad_variant_access一。这意味着标准库必须检查每个访问(libc++).做出这个决定的理由是什么?为什么它不是未定义的行为,就像C++中的其他地方一样?我可以解决它吗? 最佳答案 std::variant的当前API没有std::get的未检查版本.我不知道为什么它是这样标准化的。我说的都只是猜测。但是,您可以通过编写*std::get_if(&variant)来接近所需的行为。.如果variant