有什么方法可以包装 boost "tee" 流的构造以进行自动类型推导?
Any way to wrap construction of boost "tee" stream for automatic type deduction?
boost::iostreams::tee 和公司有一些 noisy/repetitive 用法,如下所示:
C++ "hello world" Boost tee example program
目标是制作类似的东西:
auto myTeeStream = make_tee(std::cout, myfile);
在尝试用函数 make_tee 包装此用法以允许对参数进行自动类型推导后,我意识到复制和移动构造函数都无法用于必要的类型。
所以:有没有一种合理的方法可以在 c++11 中包装 tee 流的创建?
这是我的尝试,由于删除了复制构造函数和缺少移动构造函数而无法编译:
#include <iostream>
#include <ostream>
#include <fstream>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
template <typename StreamT1, typename StreamT2>
boost::iostreams::stream<boost::iostreams::tee_device<StreamT1, StreamT2> >
make_tee(StreamT1 & t1, StreamT2 & t2)
{
using boost::iostreams::stream;
using boost::iostreams::tee;
return stream<decltype(tee(t1,t2))>(tee(t1,t2)); // compile error
//return std::move(stream<decltype(tee(t1,t2))>(tee(t1,t2))); // also fails of course
}
int main()
{
{
// desired usage
std::ofstream myFile("file.txt");
auto && myTee = make_tee(std::cout, myFile); // required from here
}
{
// noisy default usage
std::ofstream myFile("file.txt");
using boost::iostreams::tee;
using boost::iostreams::stream;
auto && myTee = stream<decltype(tee(std::cout, myFile))>(tee(std::cout, myFile));
}
return 0;
}
来自 clang++ --std=c++11 teetest.cpp
的错误是:
teetest.cpp:14:12: error: call to implicitly-deleted copy constructor of 'boost::iostreams::stream<boost::iostreams::tee_device<basic_ostream<char>, basic_ofstream<char> > >'
使用 "guaranty copy elision".
在 c++17 中编译良好
在 c++11 中,您可以使用 return {..}
:
template <typename StreamT1, typename StreamT2>
boost::iostreams::stream<boost::iostreams::tee_device<StreamT1, StreamT2> >
make_tee(StreamT1 & t1, StreamT2 & t2)
{
using boost::iostreams::stream;
using boost::iostreams::tee;
return {tee(t1,t2)};
}
boost::iostreams::tee 和公司有一些 noisy/repetitive 用法,如下所示:
C++ "hello world" Boost tee example program
目标是制作类似的东西:
auto myTeeStream = make_tee(std::cout, myfile);
在尝试用函数 make_tee 包装此用法以允许对参数进行自动类型推导后,我意识到复制和移动构造函数都无法用于必要的类型。
所以:有没有一种合理的方法可以在 c++11 中包装 tee 流的创建?
这是我的尝试,由于删除了复制构造函数和缺少移动构造函数而无法编译:
#include <iostream>
#include <ostream>
#include <fstream>
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
template <typename StreamT1, typename StreamT2>
boost::iostreams::stream<boost::iostreams::tee_device<StreamT1, StreamT2> >
make_tee(StreamT1 & t1, StreamT2 & t2)
{
using boost::iostreams::stream;
using boost::iostreams::tee;
return stream<decltype(tee(t1,t2))>(tee(t1,t2)); // compile error
//return std::move(stream<decltype(tee(t1,t2))>(tee(t1,t2))); // also fails of course
}
int main()
{
{
// desired usage
std::ofstream myFile("file.txt");
auto && myTee = make_tee(std::cout, myFile); // required from here
}
{
// noisy default usage
std::ofstream myFile("file.txt");
using boost::iostreams::tee;
using boost::iostreams::stream;
auto && myTee = stream<decltype(tee(std::cout, myFile))>(tee(std::cout, myFile));
}
return 0;
}
来自 clang++ --std=c++11 teetest.cpp
的错误是:
teetest.cpp:14:12: error: call to implicitly-deleted copy constructor of 'boost::iostreams::stream<boost::iostreams::tee_device<basic_ostream<char>, basic_ofstream<char> > >'
使用 "guaranty copy elision".
在 c++17 中编译良好在 c++11 中,您可以使用 return {..}
:
template <typename StreamT1, typename StreamT2>
boost::iostreams::stream<boost::iostreams::tee_device<StreamT1, StreamT2> >
make_tee(StreamT1 & t1, StreamT2 & t2)
{
using boost::iostreams::stream;
using boost::iostreams::tee;
return {tee(t1,t2)};
}