为什么std :: pair <char,std :: string =“”>有std :: copy到ostream的问题

时间:2018-02-01 22:59:02

标签: c++

如果我删除评论,有人可以解释为什么下面的代码不能编译吗?

    #include <iostream>
    #include <string>
    #include <utility>
    #include <vector>
    #include <iterator>
    #include <algorithm>

    std::pair<char, std::string> uncons(const std::string & s) {
        std::string::size_type len = s.length();

        if (len == 1) {
            return std::make_pair(s[0], "");
        } else if (len > 1) {
            return std::make_pair(s[0], s.substr(1));
        }

        throw "Ooops, empty string";
    }

    std::ostream& operator<<(std::ostream & out, const std::pair<char, std::string> & p) {
        if (p.second.length() > 0) {
            out << "(\'" << p.first << "\', \"" << p.second << "\")";
        }
        return out;
    }

    int main(int argc, char ** argv) {

        auto ans = uncons("G4143");

        std::vector<std::pair<char, std::string> >  vec{ans};

        //If I remove the comments below the code fails to compile (GNU's g++14))
        /*std::copy
                (
                vec.begin(),
                vec.end(),
                std::ostream_iterator<std::pair<char, std::string> >(std::cout, "\n")
                );*/

        return 0;
    }

    /*

    In file included from /usr/include/c++/5/iterator:66:0,
                     from main.cpp:6:
    /usr/include/c++/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::pair<char, std::__cxx11::basic_string<char> >; _CharT = char; _Traits = std::char_traits<char>]’:
    /usr/include/c++/5/bits/stl_algobase.h:340:18:   required from ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = std::pair<char, std::__cxx11::basic_string<char> >*; _OI = std::ostream_iterator<std::pair<char, std::__cxx11::basic_string<char> > >]’
    /usr/include/c++/5/bits/stl_algobase.h:402:44:   required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = std::pair<char, std::__cxx11::basic_string<char> >*; _OI = std::ostream_iterator<std::pair<char, std::__cxx11::basic_string<char> > >]’
    /usr/include/c++/5/bits/stl_algobase.h:438:45:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<std::pair<char, std::__cxx11::basic_string<char> >*, std::vector<std::pair<char, std::__cxx11::basic_string<char> > > >; _OI = std::ostream_iterator<std::pair<char, std::__cxx11::basic_string<char> > >]’
    /usr/include/c++/5/bits/stl_algobase.h:471:8:   required from ‘_OI s/stream_iterator.h:198:13: note:   ‘const std::pair<char, 

....
std::__cxx11::basic_string<char> >’ is not derived from ‘const std::piecewise_constant_distribution<_RealType>’
      *_M_stream << __value;
                 ^
    In file included from /usr/include/c++/5/random:51:0,
                     from /usr/include/c++/5/bits/stl_algo.h:66,
                     from /usr/include/c++/5/algorithm:62,
                     from main.cpp:7:
    /usr/include/c++/5/bits/random.tcc:3160:5: note: candidate: template<class _RealType, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::piecewise_linear_distribution<_RealType>&)
         operator<<(std::basic_ostream<_CharT, _Traits>& __os,
         ^
    /usr/include/c++/5/bits/random.tcc:3160:5: note:   template argument deduction/substitution failed:
    In file included from /usr/include/c++/5/iterator:66:0,
                     from main.cpp:6:
    /usr/include/c++/5/bits/stream_iterator.h:198:13: note:   ‘const std::pair<char, std::__cxx11::basic_string<char> >’ is not derived from ‘const std::piecewise_linear_distribution<_RealType>’
      *_M_stream << __value; 
    */

现在,如果我将std :: pair的std :: string部分包装在一个简单的类(MyString)中,那么一切都按预期工作。下面的代码编译没有问题,并按预期工作。

#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include <algorithm>

class MyString {

public:

    MyString(const std::string & s):str(s) {}

    std::string str;

};

std::pair<char, MyString> uncons(const std::string & s) {
    std::string::size_type len = s.length();

    if (len == 1) {
        return std::make_pair(s[0], MyString(""));
    } else if (len > 1) {
        return std::make_pair(s[0], MyString(s.substr(1)));
    }

    throw "Ooops, empty string";
}

std::ostream& operator<<(std::ostream & out, const std::pair<char, MyString> & p) {
    if (p.second.str.length() > 0) {
        out << "(\'" << p.first << "\', \"" << p.second.str << "\")";
    }
    return out;
}

int main(int argc, char ** argv) {

    auto ans = uncons("G4143");

    std::vector<std::pair<char, MyString> >  vec{ans};


    std::copy
            (
            vec.begin(),
            vec.end(),
            std::ostream_iterator<std::pair<char, MyString> >(std::cout, "\n")
            );

    return 0;
}

有人能说清楚为什么会这样吗?我按照建议修改了我的帖子,并尝试将错误信息全部封闭,但论坛不允许编译器禁用整个消息。

1 个答案:

答案 0 :(得分:3)

与往常一样,它是ADL。

operator<<是通过ADL找到的,因此在关联的命名空间中。由于std::string的关联命名空间仅仅是命名空间::std(声明它和特征模板参数类型参数),因此 必须定义operator<<重载那个名称空间。

事实上,如果你破解它并强制你的重载进入std命名空间,它就会起作用&#34;:http://coliru.stacked-crooked.com/a/7c2486ae963c2a64警告执行此操作不是合法

一旦自定义类型换行,该类型的声明命名空间就会参与ADL查找。

额外我为基元或非用户定义类型(可选择一般)提供自定义流的常用做法是创建一个操纵器来执行此操作,请参阅例如Template specialization (boost::lexical_cast)