Улучшенный вариант: ошибка компиляции apply_visitor

Этот простой пример кода для boost :: option и 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.

Для жизни я не могу понять проблему. Я пытался на самом деле иметь тип возвращаемого значения (хотя посетителю печати он не нужен), но в итоге я получил ту же ошибку.

Любое понимание будет оценено.

4

Решение

Вы попали под самый неприятный разбор правило: e на самом деле функция. Добавьте дополнительную пару скобок:

Expression e((ExprTrue()));
6

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector