了解boost :: variant

Baz*_*Baz 3 c++ boost

为什么不编译以下内容:

void f(int8_t a)
{
}

void f(int16_t a)
{
}

typedef boost::variant<int8_t, int16_t> AttrValue;

int main()
{
    AttrValue a;
    a = int8_t(1);
    f(a);
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

error C2665: 'f' : none of the 2 overloads could convert all the argument types
could be 'void f(int8_t)'
or 'void f(int16_t)'
Run Code Online (Sandbox Code Playgroud)

但是,这没关系:

std::cout << a; // f(a);
Run Code Online (Sandbox Code Playgroud)

哪里是std :: ostream&operator <<(std :: ostream&,const AttrValue&)在哪里定义,为什么定义它?

eca*_*mur 9

重载解析发生在编译时,当您的boost::variant实例可能包含任何类型时,编译器无法知道是否调用void f(int8_t)void f(int16_t).

std::cout << a因为在任何一种情况下它都调用相同的函数std::ostream &operator<<(std::ostream &, const AttrValue &),它在内部调度实例的运行时类型.

您需要编写访问者来执行调度:

struct f_visitor: public boost::static_visitor<void>
{
    template<typename T> void operator()(T t) const { return f(t); }
};

boost::apply_visitor(f_visitor(), a);
Run Code Online (Sandbox Code Playgroud)