Tag: Type Trait
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 […]
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 […]
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 […]
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 […]
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<> […]
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 […]
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 […]
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 […]
Recent Comments