• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    class template
    <type_traits>

    std::is_pod

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

    このトレイトはC++20で非推奨になった。同時にPODという概念自体が非推奨になった。

    概要

    TがPOD型 (Plain Old Data) か調べる。POD型は、トリビアル型、かつスタンダードレイアウト型、およびそのcv修飾を含む。

    要件

    remove_all_extents<T>::typeは、完全型か、const/volatile修飾された(あるいはされていない)voidでなければならない。

    効果

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

    備考

    "Plain Old Data"という名称はつまり、C言語の構造体や共用体と互換性を持つためである。

    非推奨の詳細 (C++20)

    C++11でPODをトリビアルとスタンダードレイアウトという2つの概念に分離した。
    そして実用上必要だったのはトリビアル型ではなくトリビアルコピー可能型、もしくはスタンダードレイアウト型であった。
    結果として、トリビアル型かつスタンダードレイアウト型というあまりに強すぎる制約を課すPODというのは、もはや定義だけされて使われていないものとなっていた。 したがって、規格書の文面でPODという用語に依存している部分は他の説明に書き換え、非推奨にすることとした。

    #include <type_traits>
    
    static_assert(std::is_pod<int>::value == true, "value == true, int is POD");
    static_assert(std::is_same<std::is_pod<int>::value_type, bool>::value, "value_type == bool");
    static_assert(std::is_same<std::is_pod<int>::type, std::true_type>::value, "type == true_type");
    static_assert(std::is_pod<int>() == true, "is_pod<int>() == true");
    
    static_assert(std::is_pod<int&>::value == false, "value == false, int& is not POD");
    static_assert(std::is_same<std::is_pod<int&>::value_type, bool>::value, "value_type == bool");
    static_assert(std::is_same<std::is_pod<int&>::type, std::false_type>::value, "type == false_type");
    static_assert(std::is_pod<int&>() == false, "is_pod<int&>() == false");
    
    static_assert(std::is_pod<const volatile int>::value == true, "const volatile int is POD");
    
    struct POD_struct{};
    struct non_POD_struct {
      non_POD_struct() {}    // デフォルトコンストラクタが非トリビアル
    };
    static_assert(std::is_pod<POD_struct>::value == true, "POD_struct is POD");
    static_assert(std::is_pod<non_POD_struct>::value == false, "non_POD_struct is not POD");
    
    int main(){}
    

    出力

    バージョン

    言語

    • C++11

    処理系

    • GCC: 4.3.4 , 4.5.3 , 4.6.2 , 4.7.0
    • Visual C++: 2008 (std::tr1) , 2010 , 2012 , 2013 , 2015
      • 2008~2012は、voidとPODなクラスへの参照(上記例のうちPOD_struct&)において、誤ってfalse_typeになっている。
      • 2013のみは、組込型への参照において、誤ってfalse_typeになっている。

    備考

    上の例でコンパイラによってはエラーになる。GCC 4.3.4, 4.5.3, Visual C++ 2010 は integral_constantoperator bool() を持っていないためエラーになる。

    参照