Tag: Type Trait

std::is_scoped_enum<>

Written by on August 13, 2023 in C++, Programming with 0 Comments
std::is_scoped_enum<>

The type trait “std::is_scoped_enum<T>::value” was introduced in C++23 to check whether the type “T” is a scoped enum type. Another way to use this is std::is_scoped_enum_v<T>. Before getting into this trait in detail, let us briefly recap the differences between unscoped and scoped enums. Unscoped Enums Unscoped enums are the old-style enums. Look at the […]

Continue Reading

std::is_standard_layout<>

Written by on June 21, 2020 in C++, Programming with 0 Comments
std::is_standard_layout<>

In our last post, we learnt about the type trait std::is_trivial<T>. Today, let us go through another type trait that is quite similar. The expression is_standard_layout<T>::value returns true if the layout of objects of type T is compiler independent, and hence is of standard format. Else, it returns false. This is important if we create objects […]

Continue Reading

std::is_trivial<>

Written by on June 6, 2020 in C++, Programming with 0 Comments
std::is_trivial<>

Today let us try to understand the type trait std::is_trivial<T>. This trait checks if the given type is a trivial type. For a precise definition of what trivial means, please visit the official page. As usual, we will go through three cases: – Primitive types – Classes without inheritance – Classes involving inheritance Case-1: Primitive […]

Continue Reading

std::is_convertible<>

Written by on May 24, 2020 in C++, Programming with 0 Comments
std::is_convertible<>

The type trait is_convertible<From, To> checks if an object of type From can be “implicitly” converted to an object of type To. The expression is_convertible<From, To>::value returns true if implicit conversion is possible, else it returns false. For more details, please check out the reference. Let us look at three primary cases. Case-1: Pre-defined Types […]

Continue Reading

dynamic_cast<> vs. std::is_base_of<>

Written by on May 9, 2020 in C++, Programming with 0 Comments
dynamic_cast<> vs. std::is_base_of<>

In the last article, we looked at the std:is_base_of<T1, T2> type trait. One question that a reader asked since that article appeared is “How does is_base_of<> differ from the dynamic_cast<> operator?” Good question! In today’s post let me try to address the key differences between the two. I am not going to cover the dynamic_cast<> […]

Continue Reading

std::is_base_of<>

Written by on April 26, 2020 in C++, Programming with 0 Comments
std::is_base_of<>

In our on-going series on C++ Type Traits, today’s topic is about the trait std::is_base_of<>. For the official description, see this. std::is_base_of<A, B>::value takes two arguments, both classes (or structs), and returns true if A is a base class of B and false otherwise. Trivially, std::<A, A>::value is true. Let us look at an example […]

Continue Reading

std::common_type<> Type Trait

Written by on April 12, 2020 in C++, Programming with 0 Comments
std::common_type<> Type Trait

In today’s post, I would like to go over the type trait std::common_type<>. This trait was introduced in C++11. As per the specification, std::common_type<T1, T2, …Tn>::type refers to a type Tx in the given list, which the rest of the types in the list can be implicitly converted to. This works with built-in as well […]

Continue Reading

std::is_empty

Written by on March 28, 2020 in C++, Programming with 0 Comments
std::is_empty<T>

In the previous post, we looked at the std::is_destructible<T> type trait. Today, let us try to understand another type trait std::is_empty<T>. As per the specification, is_empty<T>::value will return true in the following cases: – The class/struct has no non-static data member – The class/struct does not define a virtual function – The class/struct does not […]

Continue Reading

Top