【发布时间】:2014-04-25 18:39:24
【问题描述】:
我想测试一个简单的东西,如下所示:
#include <iostream>
#include <boost/variant.hpp>
template<typename T1,typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1,T2>& dt){
os << dt.first << dt.second;
return os;
}
int main(){
boost::variant<int, std::pair<int,int>, bool> v;
v = std::pair<int,int>(3,3);
std::cout << v << std::endl;
}
这应该确实有效,因为对于普通类型,如int, double 等,它可以编译。
boost::variant 有一个打印机访问器,它在内部使用它来将内容输出到流中。
实际上这无法编译,但我真的不知道问题所在:
代码在此处失败:在 variant_io.hpp 中
template <typename OStream>
class printer
: public boost::static_visitor<>
{
private: // representation
OStream& out_;
public: // structors
explicit printer(OStream& out)
: out_( out )
{
}
public: // visitor interface
template <typename T>
void operator()(const T& operand) const
{
out_ << operand; // HEEEEEEERRRRREE!!!!!!!!!!!!
}
private:
printer& operator=(const printer&);
};
附上信息:
/usr/local/include/boost/variant/detail/variant_io.hpp|64|error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
有人知道我做错了什么,为什么?
非常感谢!
【问题讨论】:
-
这不是我们需要知道才能诊断问题的地方。当模板定义出现问题时,编译器会打印扩展链。我们需要完整的输出来告诉你发生了什么。
-
由于错误消息似乎来自 MSC++,我会提到您需要从 Visual Studio 的“输出”选项卡中获取消息,“错误”窗口已过滤掉注释并对目的没有用。注释紧跟错误消息。
-
think...think...think... 这次的答案其实很简单。不过,这不是草率问题的借口;每当询问编译问题时,complete 编译器输出是mandatory。
-
抱歉转储问题:我在上面的问题中添加了解决方案,在下面的其他帖子中也提到了
-
@Gabriel 请不要编辑您的问题以让其他人知道问题已解决。将答案标记为已接受。
标签: c++11 operator-overloading ostream boost-variant