为什么不提升元组运算符==和<<编译? [英] why don't boost tuple operator == and << compile?

查看:74
本文介绍了为什么不提升元组运算符==和<<编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此boost :: tuples文档页面说:

Tuples将运算符==,!=,<,>,< =和> =简化为相应的基本运算符.这意味着,如果在两个元组的所有元素之间定义了这些运算符中的任何一个,则在元组之间也将定义相同的运算符.

Tuples reduce the operators ==, !=, <, >, <= and >= to the corresponding elementary operators. This means, that if any of these operators is defined between all elements of two tuples, then the same operator is defined between the tuples as well.

它也说:

全局运算符<<已为std :: ostream重载,以便通过递归调用operator <<<每个元素.

The global operator<< has been overloaded for std::ostream such that tuples are output by recursively calling operator<< for each element.

此处给出的示例代码看起来应该可以轻松使用.

The example code given there looks like these features should be trivially usable.

那么为什么这些代码无法编译?

So why does this code fail to compile?

#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上的行为相似.

Similar behavior from both libboost-1.54 and 1.58, g++-4.8.4 and 5.4.0 on ubuntu 14.04 and 16.04 respectively.

推荐答案

您链接到的页面也显示

要使用该库,只需添加:

To use the library, just include:

#include "boost/tuple/tuple.hpp"

比较运算符可以包含在以下内容中:

Comparison operators can be included with:

#include "boost/tuple/tuple_comparison.hpp"

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

To use tuple input and output operators,

#include "boost/tuple/tuple_io.hpp"

这篇关于为什么不提升元组运算符==和&lt;&lt;编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆