[英]Can I make std::pair of two std::ostream-s?
I'd like to make a pair containing two std::ostream
object.我想做一对包含两个std::ostream
object。 I need this so that I can pass a single object to various functions instead of two std::ostream
objects.我需要这个,以便我可以将单个 object 传递给各种函数,而不是两个std::ostream
对象。 I was hoping I could do this :我希望我能做到这一点:
#include <iostream>
#include <ostream>
using namespace std;
int main(){
std::ostream output( std::cout.rdbuf());
std::ostream error(std::cout.rdbuf());
using Logger = std::pair< std::ostream, std::ostream >;
Logger logger = std::make_pair( output, error );
return 0;
}
But I get a compiler error that there isn't a matching constructor for pair<std::basic_ostream<char>, std::basic_ostream<char> >
但我得到一个编译器错误,没有匹配的构造函数pair<std::basic_ostream<char>, std::basic_ostream<char> >
The problem that you're having is that ostream
can't be copied.您遇到的问题是无法复制ostream
。
The solution is to use a pair of references:解决方案是使用一对引用:
using Logger = std::pair<std::ostream &, std::ostream &>;
Logger logger { output, error };
Instead of copying, which is not supported for streams, you can make pair of references to them您可以对它们进行一对引用,而不是流不支持的复制
auto logger{::std::tie(output, error)};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.