名前空間
変種
操作

std::is_scalar

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
is_scalar
(C++11)
型のプロパティ
(C++11)
(C++11)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
型の変更
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
 
ヘッダ <type_traits> で定義
template< class T >
struct is_scalar;
(C++11以降)

std::is_scalarUnaryTypeTrait です。

もし Tスカラ型 であれば、メンバ定数 valuetrue になります。それ以外の型の場合、valuefalse になります。

もしプログラムが 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]
T がスカラ型であれば true、そうでなければ false
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、value を返します。
(public member function)
operator()
(C++14)
value を返します。
(public member function)

メンバ型

定義
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)
型が列挙型であるかをチェックする
(クラステンプレート) [編集]
型がポインタ型であるかをチェックする
(クラステンプレート) [編集]
型が非静的メンバ関数またはオブジェクトへのポインタであるかをチェックする
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)