我正在尝试编写一个流运算符<<
,它可以输出到一个std::tuple
流而不是一个流。所以基本上,我正在尝试tee
用 C++ 编写 Unix 命令,并执行以下操作:
std::tie(std::cout,std::clog) << 1;
我尝试使用 c++11 中的可变参数模板编程递归地编写流运算符。到目前为止,我所拥有的代码如下所示。但是代码无法编译,并且错误消息很长。
我的问题是,如何修复代码以使其正常工作?
g++ -std=c++11
使用(gcc-4.8.1)编译的第一条错误消息是:
test.cpp:24:33: error: no match for 'operator<<' (operand types are 'std::tuple<std::basic_ostream<char, std::char_traits<char> >&, std::basic_ostream<char, std::char_traits<char> >&>' and 'int')
std::tie(std::cout,std::cout) << 1;
PS我搜索了SO,并且有一些关于编写复合流对象的帖子。代码涉及到很多stream和streambuf的内部结构。我在这里寻求的是一个简单/天真的解决方案,用十几行来完成类似的任务。
谢谢
我的代码:
#include <iostream>
#include <tuple>
template<typename _Value, typename Head, typename ... Tail>
struct _tee_stream {
static std::tuple<Head,Tail...>& _print(std::tuple<Head,Tail...>& _s, const _Value& _t) {
return std::make_tuple(std::get<0>(_s) << _t ,_tee_stream<Tail...,_Value>::_print(_s,_t));
}
};
template<typename _Value>
struct _tee_stream<_Value, std::tuple<>> {
static std::tuple<>& _print(std::tuple<>& _s, const _Value& _t) {
return _s;
}
};
template< typename _Value, typename... _TElements>
std::tuple<_TElements...>& operator<<(std::tuple<_TElements...>& _s, const _Value& _t) {
return _tee_stream<_Value, std::tuple<_TElements...>>::_print(_s, _t);
};
int main() {
std::tie(std::cout,std::cout) << 1; //no compile
std::make_tuple(std::cout,std::cout) << 1; //no compile either
}
更新: 以下是基于@KerrekSB 的示例代码对我有用的:
#include <iostream>
#include <tuple>
#include <utility>
#include <fstream>
template <unsigned int N>
struct tee_stream
{
template <typename ...Args, typename T>
static std::tuple<Args...> & print(std::tuple<Args...> & t, T && x)
{
std::get<sizeof...(Args) - N>(t) << x;
tee_stream<N - 1>::print(t, std::forward<T>(x));
return t;
}
};
template <>
struct tee_stream<0>
{
template <typename ...Args, typename T>
static std::tuple<Args...> & print(std::tuple<Args...> &, T &&) {}
};
template <typename ...Args, typename T>
std::tuple<Args...> & operator<<(std::tuple<Args...> & t, T && x)
{
return tee_stream<sizeof...(Args)>::print(t, std::forward<T>(x));
}
template <typename ...Args, typename T>
std::tuple<Args...> & operator<<(std::tuple<Args...> && t, T && x)
{
return tee_stream<sizeof...(Args)>::print(t, std::forward<T>(x));
}
int main()
{
std::ofstream os("a.txt");
auto t = std::tie(std::cout, os);
t << "Foo" << "Bar\n";
std::tie(std::cout, os) << "Foo" << "Bar\n";
}
@贾罗德42,
非常感谢。您的代码比我的原型更紧凑地执行递归/循环,并修复了我的代码中引用类型的使用。原始测试用例与您的演示一样工作。std::endl
但是,如果我在流中使用(而不是“Foo”)或std::ofstream
(而不是) ,我仍然无法编译您的版本std::cout
,如下面的“not OK”行所示。知道如何解决这些问题吗?
#include <iostream>
#include <fstream>
#include <tuple>
#if 1 // Not in C++11 // make_index_sequence
#include <cstdint>
template <std::size_t...> struct index_sequence {};
template <std::size_t N, std::size_t... Is>
struct make_index_sequence : make_index_sequence<N - 1, N - 1, Is...> {};
template <std::size_t... Is>
struct make_index_sequence<0u, Is...> : index_sequence<Is...> {};
#endif // make_index_sequence
namespace detail
{
template <typename Tuple, typename T, std::size_t...Is>
Tuple output(const Tuple& t, const T& x, index_sequence<Is...>) {
return Tuple{(std::get<Is>(t) << x)...};
}
}
template <typename ...Args, typename T>
std::tuple<Args&...> operator<<(const std::tuple<Args&...>& t, const T& x) {
return detail::output(t, x, make_index_sequence<sizeof...(Args)>());
}
int main() {
std::ofstream os("aa.txt");
os << "Hi" << std::endl;
std::tie(std::cout, std::cout) << "Foo" << "Bar"; //OK
std::tie(std::cout, std::cout) << "Foo" << "Bar" << std::endl; //not OK on endl
std::tie(std::cout, os) << 1 << "Foo" << "Bar"; //not OK on ofstream
return 0;
}