C ++如何制作流的元组?

时间:2016-08-12 20:04:49

标签: c++ tuples ofstream

我有三个std :: ofstream对象,我把它们放在像下面这样的函数中,并在main()中使用它们。我编译时收到了很多错误信息,有人可以帮我吗?

#include<string>
#include<tuple>
#include<fstream>
#include<iostream>

std::tuple<std::ofstream, std::ofstream, std::ofstream>
set_ofstream_tuple() {
   std::ofstream out1("out1.txt", std::ofstream::out);
   out1 << "this is out1.txt" << std::endl;
   std::ofstream out2("out2.txt", std::ofstream::out);
   out2 << "this is out2.txt" << std::endl;
   std::ofstream out3("out3.txt", std::ofstream::out);
   out3 << "this is out3.txt" << std::endl;

   return std::make_tuple(out1, out2, out3);
}

int main() {
  std::ofstream out1, out2, out3;
  std::tie(out1, out2, out3) = set_ofstream_tuple();
  /// make use of there ofstreams more here
}

2 个答案:

答案 0 :(得分:4)

这一行:

return std::make_tuple<out1, out2, out3>;

很奇怪。你想做什么? :)

std::make_tuple是一个函数,它从参数中生成一个元组:

return std::make_tuple(std::move(out1), std::move(out2), std::move(out3));

请注意std::move,这些是必需的,因为您无法复制std::ofstream

答案 1 :(得分:1)

问题是你在返回之前将不可复制的ofstream复制到元组中。

一种解决方案是在返回之前将ofstreams移动到元组中:

#include<string>
#include<tuple>
#include<fstream>
#include<iostream>

std::tuple<std::ofstream, std::ofstream, std::ofstream>
set_ofstream_tuple() 
{
   std::ofstream out1("out1.txt", std::ofstream::out);
   out1 << "this is out1.txt" << std::endl;
   std::ofstream out2("out2.txt", std::ofstream::out);
   out2 << "this is out2.txt" << std::endl;
   std::ofstream out3("out3.txt", std::ofstream::out);
   out3 << "this is out3.txt" << std::endl;

   return std::make_tuple(std::move(out1), std::move(out2), std::move(out3));
}

int main() {
  std::ofstream out1, out2, out3;
  std::tie(out1, out2, out3) = set_ofstream_tuple();
  /// make use of there ofstreams more here

}

证明:on godbolt

另一种解决方案是在创建过程中使用元组作为主存储:

#include<string>
#include<tuple>
#include<fstream>
#include<iostream>

std::tuple<std::ofstream, std::ofstream, std::ofstream>
set_ofstream_tuple() 
{
  auto streams = std::make_tuple(std::ofstream("out1.txt", std::ofstream::out),
                                 std::ofstream("out2.txt", std::ofstream::out),
                                 std::ofstream("out3.txt", std::ofstream::out));

  auto& out1 = std::get<0>(streams);
  auto& out2 = std::get<1>(streams);
  auto& out3 = std::get<2>(streams);

  out1 << "this is out1.txt" << std::endl;
  out2 << "this is out2.txt" << std::endl;
  out3 << "this is out3.txt" << std::endl;

  return streams;
}

int main() {
  std::ofstream out1, out2, out3;
  std::tie(out1, out2, out3) = set_ofstream_tuple();
  /// make use of there ofstreams more here

}

从c ++ 17开始,我们将能够避免使用即将到来的ofstream(此处使用std::optional进行演示)重复构建空std::experimental}:

#include<string>
#include<tuple>
#include<fstream>
#include<iostream>

#include <experimental/optional>

std::tuple<std::ofstream, std::ofstream, std::ofstream>
set_ofstream_tuple() 
{
  auto streams = std::make_tuple(std::ofstream("out1.txt", std::ofstream::out),
                                 std::ofstream("out2.txt", std::ofstream::out),
                                 std::ofstream("out3.txt", std::ofstream::out));

  auto& out1 = std::get<0>(streams);
  auto& out2 = std::get<1>(streams);
  auto& out3 = std::get<2>(streams);

  out1 << "this is out1.txt" << std::endl;
  out2 << "this is out2.txt" << std::endl;
  out3 << "this is out3.txt" << std::endl;

  return streams;
}

int main() {
  std::experimental::optional<std::ofstream> out1, out2, out3;
  std::tie(out1, out2, out3) = set_ofstream_tuple();
  /// make use of there ofstreams more here

}