用于vector <vector <double >>的ostream_iterator

时间:2018-06-27 17:50:33

标签: c++ c++11

我试图理解为什么以下代码无法编译

#include <vector>
#include <ostream>
#include <iterator>
#include <iostream>

template <class ostream>
ostream& operator<<(ostream& o, const std::vector<double>& data)
{
    std::copy(data.begin(), data.end(), std::ostream_iterator<double>(o, " "));
    return o;
}

template<class ostream>
ostream& operator<<(ostream& o, const std::vector<std::vector<double>>& data)
{
    std::copy(data.begin(), data.end(), std::ostream_iterator<std::vector<double>>(o, "\n"));
    return o;
}

int main(int argc, char **argv)
{
    std::vector<std::vector<double>> vecvec = {{1,2,3}, 
                                               {4,5,6}};
    std::cout << vecvec << std::endl;
}

我认为,由于我为operator<<定义了vector<double>,因此我应该能够利用ostream_iterator

相反,我收到了编译错误,如果我将代码更改为以下代码,则一切都可以正常编译。

#include <vector>
#include <ostream>
#include <iterator>
#include <iostream>

template <class ostream>
ostream& operator<<(ostream& o, const std::vector<double>& data)
{
    std::copy(data.begin(), data.end(), std::ostream_iterator<double>(o, " "));
    return o;
}

template<class ostream>
ostream& operator<<(ostream& o, const std::vector<std::vector<double>>& data)
{
    /** changed to manually looping **/
    for (const auto& line : data)
    {
        o << line << "\n";
    }
    return o;
}

int main(int argc, char **argv)
{
    std::vector<std::vector<double>> vecvec = {{1,2,3}, 
                                               {4,5,6}};
    std::cout << vecvec << std::endl;
}

我在做什么错?

最重要的是...谁能向我解释为什么ostream_iterator在这里编译失败?

我可以找到一种解决方法并解决问题,但似乎我还没有完全了解ostream_iterator的工作原理

这是编译器(gcc 4.8.5)的输出

In file included from /opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/iterator:66:0,from <source>:3:

/opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/bits/stream_iterator.h: In instantiation of 'std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = std::vector<double>; _CharT = char; _Traits = std::char_traits<char>]':

/opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/bits/stl_algobase.h:335:18:   required from 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = std::vector<double>*; _OI = std::ostream_iterator<std::vector<double> >]'

/opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/bits/stl_algobase.h:390:70:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = std::vector<double>*; _OI = std::ostream_iterator<std::vector<double> >]'

/opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/bits/stl_algobase.h:428:38:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<std::vector<double>*, std::vector<std::vector<double> > >; _OI = std::ostream_iterator<std::vector<double> >]'

/opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/bits/stl_algobase.h:460:17:   required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<std::vector<double>*, std::vector<std::vector<double> > >; _OI = std::ostream_iterator<std::vector<double> >]'

<source>:17:96:   required from 'ostream& operator<<(ostream&, std::vector<std::vector<double> >&) [with ostream = std::basic_ostream<char>]'

<source>:26:18:   required from here

/opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/bits/stream_iterator.h:198:13: error: cannot bind 'std::ostream_iterator<std::vector<double> >::ostream_type {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

  *_M_stream << __value;

             ^

In file included from <source>:2:0:

/opt/compiler-explorer/gcc-4.8.5/include/c++/4.8.5/ostream:602:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::vector<double>]'

     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)

     ^

Compiler returned: 1

1 个答案:

答案 0 :(得分:0)

std::ostream_iterator与您的代码不兼容的原因是,您定义的operator<<在全局命名空间中,而不在命名空间std中。

std::ostream_iterator is defined像执行argument-dependent lookup(ADL)的operator<<一样呼叫ostr << value。在这种情况下,ADL找不到任何operator<<,因为在命名空间`std中的operator<<std::ostream&之间没有定义std::vector<double>

在ADL找到关联的名称空间和类之后,重载集将与unqualified name lookup找到的名称空间和类合并。要在不合格的名称查找中引用cppreference:

  

[N]相似查找按如下所述检查范围,直到找到至少一个任何类型的声明为止,这时查找停止并且不再检查任何范围。

重点已添加

由于std::ostream_iteratorstd名称空间内,因此它对ostr << value的调用在std名称空间内。在operator<<命名空间中定义了其他std,因此,不合格的名称查找将找到operator<<并停止。这种情况发生在之前考虑了发现的功能是否兼容。


请勿将运算符添加到您不拥有的类型中,除非您拥有其他类型。为了正确使用该语言,必须在与该类型相同的名称空间中定义运算符(以便可以通过ADL找到它)。但是,将函数添加到您不拥有的命名空间中并不是一种好习惯,这会引起问题。对于std,实际上是undefined behavior