最終更新日時(UTC):
が更新

履歴 編集

function
<functional>

std::function::operator=(C++11)

function& operator=(const function& f);               // (1)
function& operator=(function&& f);                    // (2)
function& operator=(nullptr_t);                       // (3)

template<class F>
function& operator=(F&& f);                           // (4)

template<class F>
function& operator=(reference_wrapper<F> f) noexcept; // (5)

効果

  • (1) : function(f).swap(*this)
  • (2) : *thisが持つ関数を、fのそれで置き換える。
  • (3) : *thisが有効な関数ポインタ、メンバポインタ、もしくは関数オブジェクトを持っている場合、それを解放する。
  • (4) : function(std::forward<F>(f)).swap(*this)
  • (5) : function(f).swap(*this)

戻り値

*this

例外

  • (5) : 投げない

備考

  • (4) :
    • C++14 : typename decay<F>::type型の関数オブジェクトが、パラメータとしてArgTypes...型をとり、戻り値としてR型を返さない場合、この関数はオーバーロード解決から除外される。

#include <iostream>
#include <functional>

int ident(int x) { return x; }

int main()
{
  std::function<int(int)> f;

  // 関数を代入
  f = ident;

  int result = f(1);
  std::cout << result << std::endl;
}

出力

1

バージョン

言語

  • C++11

処理系

参照