有什么方法可以包装自动类型扣除的助推"tee"流的构造?
Any way to wrap construction of boost "tee" stream for automatic type deduction?
boost::iostreams::tee 和公司有一点嘈杂/重复的使用,如下所示:
C++ "你好世界"助推发球台示例程序
目标是制作类似的东西:
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> > >'
在 c++17 中使用"guaranty copy elision">编译得很好。
在 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)};
}
演示
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 有什么方法可以包装自动类型扣除的助推"tee"流的构造?