Boost变体apply_visitor编译错误 [英] Boost variant apply_visitor compilation error

查看:443
本文介绍了Boost变体apply_visitor编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的示例代码为boost :: variant和boost :: apply_visitor:

This simple example code for boost::variant and boost::apply_visitor:

#include <boost/variant/recursive_variant.hpp>

struct ExprFalse;
struct ExprTrue;
struct ExprMaybe;

typedef boost::variant<
    ExprFalse,
    ExprTrue,
    ExprMaybe
    > Expression;

struct ExprFalse { };
struct ExprTrue { };
struct ExprMaybe { };

struct Printer : public boost::static_visitor<>
{
public:
    Printer(std::ostream& os) : m_os(os) { }
    void operator()(ExprFalse const& expr) const { m_os << "False"; }
    void operator()(ExprTrue const& expr) const { m_os << "True"; }
    void operator()(ExprMaybe const& expr) const { m_os << "Maybe"; }

private:
    std::ostream& m_os;
};

int main()
{
    Expression e(ExprTrue());
    boost::apply_visitor(Printer(std::cout), e);
    return 0;
}

产生以下编译错误:

g++-mp-4.8 -MMD -DBOOST_ALL_DYN_LINK -DBOOST_SPIRIT_USE_PHOENIX_V3 -Wall -std=c++11 -Os -O3 -g -I/o\
pt/local/include -I./  -c tools/t6.cpp -o tools/build/x86_64/objs/t6.o
In file included from /opt/local/include/boost/variant/apply_visitor.hpp:16:0,
                 from /opt/local/include/boost/variant/detail/hash_variant.hpp:23,
                 from /opt/local/include/boost/variant/variant.hpp:37,
                 from /opt/local/include/boost/variant/recursive_variant.hpp:36,
                 from tools/t6.cpp:4:
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp: In instantiation of 'typename Visi\
tor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with Visitor = Printer; Visitabl\
e = boost::variant<ExprFalse, ExprTrue, ExprMaybe>(ExprTrue (*)()); typename Visitor::result_type =\
 void]':
tools/t6.cpp:35:47:   required from here
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp:76:43: error: request for member 'a\
pply_visitor' in 'visitable', which is of non-class type 'boost::variant<ExprFalse, ExprTrue, ExprM\
aybe>(ExprTrue (*)())'
     return visitable.apply_visitor(visitor);
                                           ^
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp:76:43: error: return-statement with\
 a value, in function returning 'void' [-fpermissive]
make: *** [tools/build/x86_64/objs/t6.o] Error 1

在Mac OSX Mavericks上使用Boost版本1.55.0。

On Mac OSX Mavericks using Boost version 1.55.0.

对于我的生活,我不知道这个问题。我试过实际有一个返回类型(即使打印的访问者不需要一个),但我最后有同样的错误。

For the life of me, I cannot figure out the issue. I've tried actually having a return type (even though the print visitor doesn't need one), but I ended up with the same error.

任何洞察力赞美。

推荐答案

您被最烦琐的解析规则: e 实际上是一个函数。添加一对附加的括号:

You are hit by the most vexing parse rule: e is actually a function. Add an additional pair of parentheses:

Expression e((ExprTrue()));

这篇关于Boost变体apply_visitor编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆