LoginSignup
4
3

More than 5 years have passed since last update.

templateな引数に参照を渡す

Last updated at Posted at 2014-01-19

たとえば以下のコード、期待通りには動きません。

#include <iostream>

template<typename Function, typename T>
void do_it(Function f, T arg) { f(arg); }

int main() {
  auto twice = [](int& x) { x *= 2; };
  int n = 123;
  do_it(twice, n); // nが2倍になる...
  std::cout << n << std::endl;  
}

ダメなんですね、do_itの第二引数には(int&ではなく)intが渡るのでnを書き換えることができません。
<functional> に定義された ref, cref を通すことで 参照/const参照 を引き渡すことができます。

#include <iostream>
#include <functional>

template<typename Function, typename T>
void do_it(Function f, T arg) { f(arg); }

int main() {
  auto twice = [](int& x) { x *= 2; };
  int n = 123;
  do_it(twice, std::ref(n)); // ← 参照を渡す!
  std::cout << n << std::endl;  
}
4
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3