vector<vector<double>>的ostream_iterator

3
我正在努力理解为什么以下代码无法编译。
#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;
}

我认为既然我已经定义了vector<double>operator<<,那么我应该能够利用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

让我们在聊天中继续这个讨论 - mystery_doctor
1个回答

0

std::ostream_iterator 无法与您的代码一起工作的原因是,您定义的 operator<< 在全局命名空间中,而不在命名空间 std 中。

std::ostream_iterator 被定义 为调用像 ostr << value 这样的 operator<<,它执行参数相关查找(ADL)。在这种情况下,ADL 找不到任何 std 命名空间内定义的 std::ostream&std::vector<double> 之间的 operator<<

在 ADL 找到关联的命名空间和类之后,重载集合将与通过未限定名称查找找到的集合合并。引用 cppreference 上的未限定名称查找:

名称查找检查如下所述的作用域,直到找到至少一个任何种类的声明,此时查找停止,不再检查更多作用域。 强调添加 由于std::ostream_iterator位于std命名空间内,它对ostr << value的调用也在std命名空间内。 std命名空间内还定义了其他operator<<,因此未限定名称查找会找到一个operator<<并停止。这发生在被发现的函数被考虑是否兼容之前。

除非您拥有另一个类型,否则不要向您不拥有的类型添加运算符。为了与语言正常工作,运算符必须在与类型相同的命名空间中定义(以便可以通过ADL找到它)。但是,向您不拥有的命名空间添加函数并不是好的实践,这会导致问题。对于std的情况,实际上是未定义行为


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接