• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    class template
    <type_traits>

    std::is_function

    namespace std {
      template <class T>
      struct is_function;
    
      template <class T>
      inline constexpr bool is_function_v = is_function<T>::value; // C++17
    }
    

    概要

    Tが関数型か調べる

    効果

    is_functionは、型Tが関数型であるならばtrue_typeから派生し、そうでなければfalse_typeから派生する。

    #include <type_traits>
    
    using f = void();
    
    static_assert(std::is_function<f>::value == true, "value == true, f is function");
    static_assert(std::is_same<std::is_function<f>::value_type, bool>::value, "value_type == bool");
    static_assert(std::is_same<std::is_function<f>::type, std::true_type>::value, "type == true_type");
    static_assert(std::is_function<f>() == true, "is_function<f>() == true");
    
    static_assert(std::is_function<int>::value == false, "value == false, int is not function");
    static_assert(std::is_same<std::is_function<int>::value_type, bool>::value, "value_type == bool");
    static_assert(std::is_same<std::is_function<int>::type, std::false_type>::value, "type == false_type");
    static_assert(std::is_function<int>() == false, "is_function<int>() == false");
    
    static_assert(std::is_function<const f>::value == true, "const f is function");
    static_assert(std::is_function<volatile f>::value == true, "volatile f is function");
    static_assert(std::is_function<const volatile f>::value == true, "const volatile f is function");
    
    static_assert(std::is_function<f*>::value == false, "f* is not function");
    static_assert(std::is_function<f&>::value == false, "f& is not function");
    static_assert(std::is_function<f&&>::value == false, "f&& is not function");
    
    int main(){}
    

    出力

    バージョン

    言語

    • C++11

    処理系

    • GCC: 4.5.3 , 4.6.1 , 4.7.0
    • Clang 3.1, 3.2, 3.3
    • Visual C++: 2008 (std::tr1) , 2010 , 2012 , 2013 , 2015
      • 2010までは、関数への右辺値参照型をテンプレート実引数に渡すとコンパイルエラーになる。上記例では、std::is_function<f&&>の場合が該当する。

    備考

    上の例でコンパイラによってはエラーになる。GCC 4.3.4, 4.5.3, Visual C++ 2010 は integral_constantoperator bool() を持っていないためエラーになる。また、Visual C++ 2010 はコンパイラのバグのために関数への右辺値参照を用いるとエラーになる。

    Clang 3.1 - 3.3 では以下のような警告が出るが、これはClangのバグである。

    prog.cc:15:32: warning: qualifier on function type 'f' (aka 'void ()') has unspecified behavior
    static_assert(std::is_function<const f>::value == true, "const f is function");
                                   ^~~~~~~
    prog.cc:16:32: warning: qualifier on function type 'f' (aka 'void ()') has unspecified behavior
    static_assert(std::is_function<volatile f>::value == true, "volatile f is function");
                                   ^~~~~~~~~~
    prog.cc:17:32: warning: qualifier on function type 'f' (aka 'void ()') has unspecified behavior
    static_assert(std::is_function<const volatile f>::value == true, "const volatile f is function");
                                   ^~~~~~~~~~~~~~~~
    3 warnings generated.
    

    参照