std::is_scalar

Header: <type_traits>

std::is_scalar is a UnaryTypeTrait.

# Declarations

template< class T >
struct is_scalar;

(since C++11)

# Notes

Each individual memory location in the C++ memory model, including the hidden memory locations used by language features (e.g. virtual table pointer), has scalar type (or is a sequence of adjacent bit-fields of non-zero length). Sequencing of side-effects in expression evaluation, inter-thread synchronization, and dependency ordering are all defined in terms of individual scalar objects.

# Example

#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);
}

# See also