Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 标准::实验::奥斯特雷姆乔伊纳和标准::对_C++_Operator Overloading_C++17 - Fatal编程技术网

C++ 标准::实验::奥斯特雷姆乔伊纳和标准::对

C++ 标准::实验::奥斯特雷姆乔伊纳和标准::对,c++,operator-overloading,c++17,C++,Operator Overloading,C++17,在c++17/g++7中,终于有了久违的ostream_joiner。它允许正确输出到ostream,使用中缀分隔符分隔集合元素 #include <algorithm> #include <experimental/iterator> #include <iostream> #include <iterator> #include <vector> #include <string> using string = std

在c++17/g++7中,终于有了久违的ostream_joiner。它允许正确输出到ostream,使用中缀分隔符分隔集合元素

#include <algorithm>
#include <experimental/iterator>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>

using string = std::string;
#if 1
struct pair {
    string first;
    string second;
};
#else
using pair = std::pair<string,string>;
#endif


std::ostream& operator<<(std::ostream& lhs, const pair &p) {
    return lhs << p.first << "=" << p.second;
}

int main()
{
    std::vector<pair> pairs = {{"foo", "bar"}, {"baz", "42"}};
    std::copy(std::begin(pairs),
          std::end(pairs),
          std::experimental::make_ostream_joiner(std::cout, ", "));
}
。。。将代码段中的#if 1更改为#if 0会导致编译器抱怨缺少正确的移位运算符:

main.cpp:29:70:   required from here
/usr/local/include/c++/7.2.0/experimental/iterator:88:10: error: no match for 
'operator<<' (operand types are 
'std::experimental::fundamentals_v2::ostream_joiner<const char*, char, 
std::char_traits<char> >::ostream_type {aka std::basic_ostream<char>}' and 
'const std::pair<std::__cxx11::basic_string<char>, 
std::__cxx11::basic_string<char> >')
  *_M_out << __value;
main.cpp:29:70:从此处开始需要
/usr/local/include/c++/7.2.0/实验/迭代器:88:10:错误:与

“运算符在
ostream_joiner
的实现内部的某个地方,将尝试执行以下操作:

os << value;
或者,您也不能使用
make_ostream_joiner
。可以取代这一点:

std::copy(std::begin(pairs),
      std::end(pairs),
      std::experimental::make_ostream_joiner(std::cout, ", "));
为此:

const char* delim = "";
for (auto const& pair : pairs) {
    std::cout << delim << pair; // now, our point of definition does include
                                // our operator<<() declaration, we don't need ADL
    delim = ", ";
}
const char*delim=“”;
用于(自动常量和对:对){
标准::cout
是否可以使stream操作符在不污染std名称空间的情况下工作

如果您愿意将您的
副本
替换为
转换
,则可以-使用从中获取的
放置调用
,您可以使用:

std::transform(
    std::begin(pairs), std::end(pairs),
    std::experimental::make_ostream_joiner(std::cout, ", "),
    [](auto& p) {
        return put_invocation([&p = p](auto& os) {
            // adl works just fine here
            return os << p;
            // or `os << p.first << "=" << p.second;`
        });
    }
);
std::transform( 标准::开始(成对),标准::结束(成对), 标准::实验性::制作木匠(标准::cout,“,”, [](自动和p){ 返回put_调用([&p=p](自动操作系统)(&os){ //adl在这里工作得很好
return os谢谢,愚蠢的我-接受。但是:我们需要通过在名称空间中插入流操作符来污染名称空间,还是在这里放弃?@argonaut6x在名称空间中添加新函数std是UB,所以不要这样做。所以我们放弃了?没有其他方法可以让操作符@argonaut6x不使用
std::pair
?使用for循环而不是
>复制< /代码>和<代码> MaxyOrthulyJoier-?@ ArgAuto6x:这是两个选项。不确定为什么认为“反作用”。但是,我不喜欢它,因为它触及了<>代码> STD< /Cord>名称空间。
const char* delim = "";
for (auto const& pair : pairs) {
    std::cout << delim << pair; // now, our point of definition does include
                                // our operator<<() declaration, we don't need ADL
    delim = ", ";
}
std::transform(
    std::begin(pairs), std::end(pairs),
    std::experimental::make_ostream_joiner(std::cout, ", "),
    [](auto& p) {
        return put_invocation([&p = p](auto& os) {
            // adl works just fine here
            return os << p;
            // or `os << p.first << "=" << p.second;`
        });
    }
);