Почему я не могу зайти на этот пользовательский тип с boost :: Вариант? - PullRequest
3 голосов
/ 21 апреля 2011

Следующий код:

#include <boost/variant.hpp>
#include <iostream>
#include <string>

struct A
{
    A()
    {
    }
    ~A() throw()
    {
    }
    A& operator=(A const & rhs)
    {
        return *this;
    }

    bool operator==(A const & rhs)
    {
        return true;
    }

    bool operator<(A const & rhs)
    {
        return false;
    }
};

std::ostream & operator<<(std::ostream & os, A const & rhs)
{
    os << "A";
    return os;
}

typedef boost::variant<int, std::string, A> message_t;

struct dispatcher_t : boost::static_visitor<>
{
    template <typename T>
    void operator()(T const & t) const
    {
        std::cout << t << std::endl;
    }
};

int main(int argc, char * const * argv)
{
    message_t m("hi");
    boost::apply_visitor(dispatcher_t(), m);
    message_t a(A());
    boost::apply_visitor(dispatcher_t(), a);
}

Выводит следующую ошибку.

In file included from /usr/include/boost/variant/apply_visitor.hpp:17,
                 from /usr/include/boost/variant.hpp:24,
                 from main.cpp:2:
/usr/include/boost/variant/detail/apply_visitor_unary.hpp: In function ‘typename Visitor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with Visitor = dispatcher_t, Visitable = message_t(A (*)())]’:
main.cpp:51:   instantiated from here
/usr/include/boost/variant/detail/apply_visitor_unary.hpp:72: error: request for member ‘apply_visitor’ in ‘visitable’, which is of non-class type ‘message_t(A (*)())’
/usr/include/boost/variant/detail/apply_visitor_unary.hpp:72: error: return-statement with a value, in function returning 'void'

Сначала я просто пытался использовать очень простую букву A, но пытался удовлетворить каждый требование Boost. Варианты мест на BoundedTypes.Раньше было

struct A {};

Посетитель отлично работает со строковым значением, но даже не может скомпилировать попытку посетить A. Я использую gcc-4.4.5.Есть идеи?

1 Ответ

4 голосов
/ 21 апреля 2011
message_t a(A());

Имеет проблему наиболее неприятного анализа: объявляет функцию, а не создает переменную.Множество способов разрешения, например message_t a = A();

...