【问题标题】:why don't boost tuple operator == and << compile?为什么不提升元组运算符 == 和 << 编译?
【发布时间】: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 上的行为相似。

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    你链接的页面也说

    要使用该库,只需包括:

    #include "boost/tuple/tuple.hpp"
    

    比较运算符可以包含在:

    #include "boost/tuple/tuple_comparison.hpp"
    

    要使用元组输入和输出运算符,

    #include "boost/tuple/tuple_io.hpp"
    

    【讨论】:

    • 所以你说 RTFM 还不够,我真的需要阅读 整个 手册? :) 感谢您发现重要的一点。
    • @bstpierre 事实上,RTEFM :)
    猜你喜欢
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2021-09-09
    • 2017-04-08
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多