C++ 在C+中构造联合元组+;11

C++ 在C+中构造联合元组+;11,c++,c++11,tuples,unions,C++,C++11,Tuples,Unions,元组有点像。是否也存在行为类似于并集的元组?或者我可以访问元组中的成员的联合,例如 my_union_tuple<int, char> u; get<1>(u); get<int>(u); // C++14 only, or see below my\u union\u tuple u; 获得(u); 得到(u);//仅限C++14,或参见下文 有关第二行,请参见 当然,解决方案不仅适用于特定的联合,如,而且适用于任意类型和任意数量的类型。Nostd::t

元组有点像。是否也存在行为类似于并集的元组?或者我可以访问元组中的成员的联合,例如

my_union_tuple<int, char> u;
get<1>(u);
get<int>(u); // C++14 only, or see below
my\u union\u tuple u;
获得(u);
得到(u);//仅限C++14,或参见下文
有关第二行,请参见

当然,解决方案不仅适用于特定的联合,如
,而且适用于任意类型和任意数量的类型。

No
std::tuple

boost::变体v;
v=“你好”;

这些被称为变体。有一个实现。谢谢,听起来正确。从标题来看,我猜boost没有使用可变列表,而是使用类型列表?(C++03的等价物)@Johannes我不知道。Boost机器在很大程度上依赖于编译器,因为它们支持非常旧的机器,所以肯定有不止一个实现。
boost::variant<int, std::string> v;

v = "hello";
std::cout << v << std::endl;
class times_two_visitor
    : public boost::static_visitor<>
{
public:

    void operator()(int & i) const
    {
        i *= 2;
    }

    void operator()(std::string & str) const
    {
        str += str;
    }

};
std::string& str = boost::get<std::string>(v);