首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >了解boost::variant

了解boost::variant
EN

Stack Overflow用户
提问于 2012-08-09 20:32:19
回答 2查看 857关注 0票数 3

为什么下面的代码不能编译:

代码语言:javascript
复制
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);
}

使用编译器错误:

代码语言:javascript
复制
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)'

然而,这是可以的:

代码语言:javascript
复制
std::cout << a; // f(a);

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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 &),该函数内部对实例的运行时类型进行调度。

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

代码语言:javascript
复制
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);
票数 9
EN

Stack Overflow用户

发布于 2012-08-09 20:41:17

碰巧operator<<是为boost::variant定义的,因此编译器不需要执行任何隐式类型转换。另一方面,对于您的f()函数,它不知道要选择哪个转换。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11883537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档