Quantcast
Channel: イグトランスの頭の中
Viewing all articles
Browse latest Browse all 123

fstreamを返す関数

$
0
0

今更ですが、C++11からはfstreamやstringstreamを関数の戻り値にできるようになりました。

#include <iostream>
#include <fstream>
 
std::ifstream open_config_file()
{
  std::ifstream s("app.conf");
  s.exceptions(std::ios_base::badbit | std::ios_base::failbit);
  return std::move(s);
}
 
int main()
{
  auto f = open_config_file();
  // ここでfから読み込む処理。
}

例はifstreamですが、もちろんofstreamでも可能です。ムーブコンストラクタ・ムーブ代入演算子そしてswapメンバ関数が追加されています。

fstreamを返す関数 is a post from: イグトランスの頭の中(のかけら)


Viewing all articles
Browse latest Browse all 123

Trending Articles