std:: is_function
|
ヘッダーで定義
<type_traits>
|
||
|
template
<
class
T
>
struct is_function ; |
(C++11以降) | |
std::is_function
は
UnaryTypeTrait
です。
T
が関数型かどうかを判定します。
std::
function
、ラムダ式、
operator()
をオーバーロードしたクラス、関数ポインタなどは関数型としてカウントされません。メンバ定数
value
を提供し、
T
が関数型の場合には
true
に、それ以外の場合には
false
に等しくなります。
プログラムが
std::is_function
または
std::is_function_v
に対する特殊化を追加する場合、動作は未定義です。
目次 |
テンプレートパラメータ
| T | - | チェックする型 |
ヘルパー変数テンプレート
|
template
<
class
T
>
constexpr bool is_function_v = is_function < T > :: value ; |
(C++17以降) | |
std::integral_constantから継承
メンバ定数
|
value
[static]
|
true
T
が関数型の場合、
false
それ以外の場合
(public static member constant) |
メンバ関数
|
operator bool
|
オブジェクトを
bool
に変換、
value
を返す
(public member function) |
|
operator()
(C++14)
|
value
を返す
(public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
std:: integral_constant < bool , value > |
注記
std::is_function
はより簡潔な方法で実装可能です。以下の実装と同様の方法が、新バージョンの
libc++
、
libstdc++
および
MS STL
で採用されています:
template<class T> struct is_function : std::integral_constant< bool, !std::is_const<const T>::value && !std::is_reference<T>::value > {};
以下の実装は教育目的で示されており、多様な関数型を示すためです。
実装例
// プライマリテンプレート template<class> struct is_function : std::false_type {}; // 通常関数の特殊化 template<class Ret, class... Args> struct is_function<Ret(Args...)> : std::true_type {}; // 可変引数関数(例: std::printf)の特殊化 template<class Ret, class... Args> struct is_function<Ret(Args......)> : std::true_type {}; // cv修飾子を持つ関数型の特殊化 template<class Ret, class... Args> struct is_function<Ret(Args...) const> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile> : std::true_type {}; // 参照修飾子を持つ関数型の特殊化 template<class Ret, class... Args> struct is_function<Ret(Args...) &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile &> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile &&> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile &&> : std::true_type {}; // 上記すべてのnoexceptバージョンに対する特殊化 (C++17以降) template<class Ret, class... Args> struct is_function<Ret(Args...) noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile & noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) volatile && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args...) const volatile && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) volatile && noexcept> : std::true_type {}; template<class Ret, class... Args> struct is_function<Ret(Args......) const volatile && noexcept> : std::true_type {}; |
例
#include <functional> #include <type_traits> int f(); static_assert(std::is_function_v<decltype(f)>); static_assert(std::is_function_v<int(int)>); static_assert(!std::is_function_v<int>); static_assert(!std::is_function_v<decltype([]{})>); static_assert(!std::is_function_v<std::function<void()>>); struct O { void operator()() {} }; static_assert(std::is_function_v<O()>); struct A { static int foo(); int fun() const&; }; static_assert(!std::is_function_v<A>); static_assert(std::is_function_v<decltype(A::foo)>); static_assert(!std::is_function_v<decltype(&A::fun)>); template<typename> struct PM_traits {}; template<class T, class U> struct PM_traits<U T::*> { using member_type = U; }; int main() { using T = PM_traits<decltype(&A::fun)>::member_type; // T は int() const& 型 static_assert(std::is_function_v<T>); }
関連項目
|
指定された引数型で呼び出し可能かどうか(
std::invoke
によって呼び出されるかのように)をチェックする
(クラステンプレート) |
|
|
(C++11)
|
型がオブジェクト型かどうかをチェックする
(クラステンプレート) |
|
(C++11)
|
型が非共用体クラス型かどうかをチェックする
(クラステンプレート) |