为什么我不能使用 boost::variant 访问这个自定义类型?
以下代码:
#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.Variant 对 BoundedTypes 的每要求。 A 曾经是
struct A {};
访问者可以很好地处理字符串值,但甚至无法编译访问 A 的尝试。我正在使用 gcc-4.4.5。有什么想法吗?
The following code:
#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);
}
Yields the following error.
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'
I originally just tried using a very simple A but I was trying to satisfy every requirement Boost.Variant places on BoundedTypes. A used to be
struct A {};
The visitor works fine with the string value but can't even compile the attempt to visit A. I'm using gcc-4.4.5. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
具有最令人烦恼的解析问题:声明一个函数而不是创建一个变量。解决方法有很多,例如
message_t a = A();
Has the most-vexing-parse problem: declares a function rather than creates a variable. Many ways to resolve, e.g.
message_t a = A();