Namespaces
Variants

std:: is_scalar

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
(C++11)
is_scalar
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定義先ヘッダ <type_traits>
template < class T >
struct is_scalar ;
(C++11以降)

std::is_scalar UnaryTypeTrait です。

T scalar type である場合、メンバー定数 value true に設定します。それ以外の型の場合、 value false となります。

プログラムが std::is_scalar または std::is_scalar_v に対する特殊化を追加する場合、動作は未定義です。

目次

テンプレートパラメータ

T - チェックする型

ヘルパー変数テンプレート

template < class T >
constexpr bool is_scalar_v = is_scalar < T > :: value ;
(C++17以降)

std::integral_constantから継承

メンバ定数

value
[static]
true T がスカラ型の場合、 false それ以外の場合
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、 value を返す
(公開メンバ関数)
operator()
(C++14)
value を返す
(公開メンバ関数)

メンバ型

定義
value_type bool
type std:: integral_constant < bool , value >

注記

C++メモリモデルにおける個々のメモリ位置(言語機能で使用される隠れたメモリ位置、例えば仮想関数テーブルポインタなども含む)は、すべてスカラ型を持ちます(あるいは非ゼロ長の隣接するビットフィールドのシーケンスです)。式評価における副作用の順序付け、スレッド間同期、および依存関係の順序付けは、すべて個々のスカラオブジェクトに関して定義されます。

実装例

template<class T>
struct is_scalar : std::integral_constant<bool, std::is_arithmetic<T>::value
                                             || std::is_enum<T>::value
                                             || std::is_pointer<T>::value
                                             || std::is_member_pointer<T>::value
                                             || std::is_null_pointer<T>::value>
{};

#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <utility>
template<typename Head, typename... Tail>
void are_scalars(Head&& head, Tail&&... tail)
{
    using T = std::decay_t<decltype(head)>;
    std::cout << typeid(T).name() << " is "
              << (std::is_scalar_v<T> ? "" : "not ")
              << "a scalar\n";
    if constexpr (sizeof... (Tail))
    {
        are_scalars(std::forward<decltype(tail)>(tail)...);
    }
}
int main()
{
    struct S { int m; } s;
    int S::* mp = &S::m;
    enum class E { e };
    are_scalars(42, 3.14, E::e, "str", mp, nullptr, s);
}

出力例:

int is a scalar
double is a scalar
main::E is a scalar
char const* is a scalar
int main::S::* is a scalar
nullptr is a scalar
main::S is not a scalar

関連項目

型が算術型かどうかをチェックする
(クラステンプレート)
(C++11)
型が列挙型かどうかをチェックする
(クラステンプレート)
(C++11)
型がポインタ型かどうかをチェックする
(クラステンプレート)
型が非静的メンバ関数またはオブジェクトへのポインタかどうかをチェックする
(クラステンプレート)