为什么下面的代码不能编译:
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);
}使用编译器错误:
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)'然而,这是可以的:
std::cout << a; // f(a);哪里定义了std::ostream &operator<<(std::ostream &,const AttrValue &),为什么要定义它?
发布于 2012-08-09 20:38:53
重载解析在编译时发生,此时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);发布于 2012-08-09 20:41:17
碰巧operator<<是为boost::variant定义的,因此编译器不需要执行任何隐式类型转换。另一方面,对于您的f()函数,它不知道要选择哪个转换。
https://stackoverflow.com/questions/11883537
复制相似问题