【发布时间】:2017-04-30 02:46:13
【问题描述】:
This boost::tuples doc page 说:
元组将运算符 ==、!=、、= 简化为相应的基本运算符。这意味着,如果在两个元组的所有元素之间定义了这些运算符中的任何一个,那么在元组之间也定义了相同的运算符。
它还说:
全局运算符
那里给出的示例代码看起来这些功能应该可以轻松使用。
那么为什么这段代码编译失败呢?
#include <iostream>
#include <boost/tuple/tuple.hpp>
int main()
{
using namespace std;
using namespace boost;
using namespace boost::tuples;
tuple<int, int> t1(0, 0);
tuple<int, int> t2(0, 0);
cerr << "t1: " << t1 << endl;
cerr << "t2: " << t2 << endl;
if (t1 == t2) { cerr << "equal\n"; } else { cerr << "notequal\n"; }
return 0;
}
g++ -Wall -Wextra -Werror tuple.cxx -o tuple 我得到:
tuple.cxx: In function ‘int main()’:
tuple.cxx:12:17: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘boost::tuples::tuple<int, int>’)
cerr << "t1: " << t1 << endl;
^
以及大量相关错误。
libboost-1.54 和 1.58、g++-4.8.4 和 5.4.0 在 ubuntu 14.04 和 16.04 上的行为相似。
【问题讨论】: