名前空間
変種
操作

std::invoke

提供: cppreference.com
< cpp‎ | utility‎ | functional
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
関数オブジェクト
関数ラッパー
(C++11)
(C++11)
関数の部分適用
(C++20)
(C++11)
関数呼び出し
invoke
(C++17)
恒等関数オブジェクト
(C++20)
参照ラッパー
(C++11)(C++11)
演算子ラッパー
否定子
(C++17)
検索子
制約付き比較子
古いバインダとアダプタ
(C++17未満)
(C++17未満)
(C++17未満)
(C++17未満)
(C++17未満)(C++17未満)(C++17未満)(C++17未満)
(C++20未満)
(C++20未満)
(C++17未満)(C++17未満)
(C++17未満)(C++17未満)

(C++17未満)
(C++17未満)(C++17未満)(C++17未満)(C++17未満)
(C++20未満)
(C++20未満)
 
ヘッダ <functional> で定義
template< class F, class... Args>

std::invoke_result_t<F, Args...>

  invoke(F&& f, Args&&... args) noexcept(/* see below */);
(C++17以上)
(C++20未満)
template< class F, class... Args>

constexpr std::invoke_result_t<F, Args...>

  invoke(F&& f, Args&&... args) noexcept(/* see below */);
(C++20以上)

INVOKE(std::forward<F>(f), std::forward<Args>(args)...) によって行われたかのように、Callable なオブジェクト f を引数 args で呼び出します。

INVOKE(f, t1, t2, ..., tN) は以下のように定義されます。

  • std::is_base_of<T, std::decay_t<decltype(t1)>>::valuetrue であれば、 INVOKE(f, t1, t2, ..., tN)(t1.*f)(t2, ..., tN) と同等です。
  • std::decay_t<decltype(t1)>std::reference_wrapper の特殊化であれば、 INVOKE(f, t1, t2, ..., tN)(t1.get().*f)(t2, ..., tN) と同等です。
  • t1 が上のいずれも満たさなければ、 INVOKE(f, t1, t2, ..., tN)((*t1).*f)(t2, ..., tN) と同等です。
  • std::is_base_of<T, std::decay_t<decltype(t1)>>::valuetrue であれば、 INVOKE(f, t1)t1.*f と同等です。
  • std::decay_t<decltype(t1)>std::reference_wrapper の特殊化であれば、 INVOKE(f, t1)t1.get().*f と同等です。
  • t1 が上のいずれも満たさなければ、 INVOKE(f, t1)(*t1).*f と同等です。
  • そうでなければ、 INVOKE(f, t1, t2, ..., tN)f(t1, t2, ..., tN) と同等です (つまり、 fFunctionObject です)。

目次

[編集] 引数

f - 呼ばれる Callable なオブジェクト
args - f に渡す引数

[編集] 例外

noexcept 指定:  
noexcept(std::is_nothrow_invocable_v<F, Args...>)

[編集] 実装例

namespace detail {
template <class T>
struct is_reference_wrapper : std::false_type {};
template <class U>
struct is_reference_wrapper<std::reference_wrapper<U>> : std::true_type {};
template <class T>
constexpr bool is_reference_wrapper_v = is_reference_wrapper<T>::value;
 
template <class T, class Type, class T1, class... Args>
constexpr decltype(auto) INVOKE(Type T::* f, T1&& t1, Args&&... args)
{
    if constexpr (std::is_member_function_pointer_v<decltype(f)>) {
        if constexpr (std::is_base_of_v<T, std::decay_t<T1>>)
            return (std::forward<T1>(t1).*f)(std::forward<Args>(args)...);
        else if constexpr (is_reference_wrapper_v<std::decay_t<T1>>)
            return (t1.get().*f)(std::forward<Args>(args)...);
        else
            return ((*std::forward<T1>(t1)).*f)(std::forward<Args>(args)...);
    } else {
        static_assert(std::is_member_object_pointer_v<decltype(f)>);
        static_assert(sizeof...(args) == 0);
        if constexpr (std::is_base_of_v<T, std::decay_t<T1>>)
            return std::forward<T1>(t1).*f;
        else if constexpr (is_reference_wrapper_v<std::decay_t<T1>>)
            return t1.get().*f;
        else
            return (*std::forward<T1>(t1)).*f;
    }
}
 
template <class F, class... Args>
constexpr decltype(auto) INVOKE(F&& f, Args&&... args)
{
      return std::forward<F>(f)(std::forward<Args>(args)...);
}
} // namespace detail
 
template< class F, class... Args>
constexpr std::invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) 
  noexcept(std::is_nothrow_invocable_v<F, Args...>)
{
    return detail::INVOKE(std::forward<F>(f), std::forward<Args>(args)...);
}

[編集]

#include <functional>
#include <iostream>
 
struct Foo {
    Foo(int num) : num_(num) {}
    void print_add(int i) const { std::cout << num_+i << '\n'; }
    int num_;
};
 
void print_num(int i)
{
    std::cout << i << '\n';
}
 
struct PrintNum {
    void operator()(int i) const
    {
        std::cout << i << '\n';
    }
};
 
int main()
{
    // 自由関数を呼びます。
    std::invoke(print_num, -9);
 
    // ラムダを呼びます。
    std::invoke([]() { print_num(42); });
 
    // メンバ関数を呼びます。
    const Foo foo(314159);
    std::invoke(&Foo::print_add, foo, 1);
 
    // データメンバを呼びます (アクセスします)。
    std::cout << "num_: " << std::invoke(&Foo::num_, foo) << '\n';
 
    // 関数オブジェクトを呼びます。
    std::invoke(PrintNum(), 18);
}

出力:

-9
42
314160
num_: 314159
18

[編集] 関連項目

(C++11)
メンバポインタから関数オブジェクトを作成します
(関数テンプレート) [edit]
(C++11)(C++20で削除)(C++17)
指定された引数のセットを渡して callable なオブジェクトを呼んだときの結果の型を推定します
(クラステンプレート) [edit]
型が指定された引数型で (std::invoke によるかのように) 呼ぶことが可能かどうか調べます
(クラステンプレート) [edit]
(C++17)
タプルを引数として使用して関数を呼びます
(関数テンプレート) [edit]