Skip to content

Latest commit

 

History

History
180 lines (144 loc) · 8.99 KB

File metadata and controls

180 lines (144 loc) · 8.99 KB

コンストラクタ

  • functional[meta header]
  • std[meta namespace]
  • function_ref[meta class]
  • function[meta id-type]
  • cpp26[meta cpp]
template<class F> function_ref(F* f) noexcept;  // (1)

template<class F>
constexpr function_ref(F&& f) noexcept;  // (2)

template<auto c, class F>
constexpr function_ref(constant_wrapper<c, F> f) noexcept;  // (3)
template<auto c, class F, class U>
constexpr function_ref(constant_wrapper<c, F> f, U&& obj) noexcept;  // (4)
template<auto c, class F, class T>
constexpr function_ref(constant_wrapper<c, F> f, /*cv*/ T* obj) noexcept;  // (5)

constexpr function_ref(const function_ref&) noexcept = default;  // (6)
  • constant_wrapper[link /reference/utility/constant_wrapper.md]

概要

function_refオブジェクトを構築する。

function_refクラステンプレートパラメータのnoexcept例外指定 noex に応じて、説明用のbool型テンプレート定数is-invocable-using<T...>を次のように定義する :

function_refオブジェクトは、説明専用のメンバ変数thunk-ptrbound-entityを保持する。

また、説明専用の変数テンプレートis-convertible-from-specialization<F>を次のように定義する。型Fが、あるCV修飾 cv2 とnoexcept例外指定 noex2 に対するfunction_ref<R(Args...) /*cv2*/ noexcept(/*noex2*/)>の特殊化を表す場合、is-convertible-from-specialization<F>は次の値と等しい :

is_convertible_v<R(&)(Args...) noexcept(/*noex2*/), R(&)(Args...) noexcept(/*noex*/)> &&
is_convertible_v<int /*cv*/&, int /*cv2*/&>
  • is_convertible_v[link /reference/type_traits/is_convertible.md]

そうでない場合、is-convertible-from-specialization<F>falseである。

テンプレートパラメータ制約

function_refクラステンプレートパラメータのCV修飾子 cv に応じて

  • (1) : is_function<F>true、かつis-invocable-using<F>trueであること
  • (2) : Tremove_reference_t<F>としたとき
    • remove_cvref_t<F>function_refと同一型ではなく、かつ
    • is_member_pointer_v<T>falseであり、かつ
    • is-invocable-using</*cv*/ T&>trueであること
  • (3) : is-invocable-using<const F&>trueであること
  • (4) : Tremove_reference_t<U>としたとき
    • is_rvalue_reference_v<U&&>falseであり、かつ
    • is-invocable-using<const F&, /*cv*/ T&>trueであること
  • (5) : is-invocable-using<const F&, /*cv*/ T*>trueであること

適格要件

  • (3), (4), (5) : is_pointer_v<F> || is_member_pointer_v<F>trueならば、f.valueがヌルポインタでないこと。
  • (3) : ArgTypesが空のパックではなく、remove_cvref_t<ArgTypes>...のすべての型が定数として扱える(constant_wrapperの説明用コンセプトconstexpr-paramのモデルである)場合、constant_wrapper</*INVOKE*/(f.value, remove_cvref_t<ArgTypes>::value...)>が妥当な型ではないこと。

事前条件

  • (1) : fがヌルポインタでないこと。
  • (5) : is_member_pointer_v<F>trueのとき、objがヌルポインタでないこと。

効果

function_refクラステンプレートパラメータのCV修飾子 cv に応じて

  • (1) : bound-entityfで、thunk-ptrを説明専用の関数thunkへのアドレスで初期化する。
  • (2) : is-convertible-from-specialization<remove_cv_t<T>>falseの場合、bound-entityaddressof(f)で、thunk-ptrを説明専用の関数thunkへのアドレスで初期化する。
    • 関数呼び出しthunk(bound-entity, call-args...)invoke_r<R>(static_cast</*cv*/ T&>(f), call-args...)と等価。
    • is-convertible-from-specialization<remove_cv_t<T>>trueの場合(fが互換するシグニチャ・CV修飾をもつfunction_refの特殊化であるとき)、bound-entityfbound-entityの値で、thunk-ptrfthunk-ptrの値で初期化する。これにより、function_refから別のfunction_refを構築する際に、本来不要な二重の間接呼び出しが回避される。
  • (3) : bound-entityを未規定オブジェクトへのポインタまたはヌルポインタで、thunk-ptrを説明専用の関数thunkへのアドレスで初期化する。
  • (4) : bound-entityaddressof(obj)で、thunk-ptrを説明専用の関数thunkへのアドレスで初期化する。
  • (5) : bound-entityobjで、thunk-ptrを説明専用の関数thunkへのアドレスで初期化する。
  • (6) : コピーコンストラクタ。

例外

投げない

#include <functional>
#include <iostream>
#include <utility>

int ident_func(int x)
{ return x; }

struct ident_functor {
  int operator()(int x) const
  { return x; }
};

struct X {
  int ident_func(int x) const
  { return x; }
};


int main()
{
  // (1) 関数ポインタ
  {
    std::function_ref<int(int)> f1 = &ident_func;
    std::cout << "(1) : " << f1(1) << std::endl;
  }
  // (2) 関数オブジェクト
  {
    ident_functor functor;
    std::function_ref<int(int)> f2 = functor;
    std::cout << "(2) : " << f2(2) << std::endl;
  }
  // (3) メンバ関数
  {
    std::function_ref<int(X&, int)> f3 = std::cw<&X::ident_func>;
    X obj;
    std::cout << "(3) : " << f3(obj, 3) << std::endl;
  }
  // (4), (5) メンバ関数+オブジェクト束縛
  {
    X obj;
    std::function_ref<int(int)> f4{std::cw<&X::ident_func>, obj};
    std::cout << "(4) : " << f4(4) << std::endl;
    std::function_ref<int(int)> f5{std::cw<&X::ident_func>, &obj};
    std::cout << "(5) : " << f5(5) << std::endl;
  }
  // (6) コピーコンストラクタ
  {
    std::function_ref<int(int)> f1 = &ident_func;
    std::function_ref<int(int)> f6 = f1;
    std::cout << "(6) : " << f6(6) << std::endl;
  }
}
  • std::cw[link /reference/utility/constant_wrapper.md]

出力

(1) : 1
(2) : 2
(3) : 3
(4) : 4
(5) : 5
(6) : 6

バージョン

言語

  • C++26

処理系

参照