Questions tagged [typetraits]

Type traits are syntactic extensions that allow the developer to determine at compile time various characteristics of a type. C++ support is provided by a combination of compiler support and a set of library templates. Access to the type traits is provided via the header

Type traits are syntactic and library extensions that allow the developer to determine, at compile time, various characteristics of a type. Well known type trait implementations include the boost library and the tr1 (ISO/IEC TR 19768) library extensions published in 2007.

Documentation for type traits can be found here:

C++ STL see Microsoft Visual C++, IBM C++ and GNU C++

Compiler Support for Type Traits in GNU C++, Visual Studio 2010

1035 questions
118
votes
5 answers

How does `is_base_of` work?

How does the following code work? typedef char (&yes)[1]; typedef char (&no)[2]; template struct Host { operator B*() const; operator D*(); }; template struct is_base_of { template
Alexey Malistov
  • 24,755
  • 13
  • 60
  • 84
97
votes
1 answer

Why is std::is_pod deprecated in C++20?

std::is_pod will be probably deprecated in C++20. What's the reason for this choice? What should I use in place of std::is_pod to know if a type is actually a POD?
skypjack
  • 45,296
  • 16
  • 80
  • 161
78
votes
3 answers

trivial vs. standard layout vs. POD

In layman's terms, what's the difference between trivial types, standard layout types and PODs? Specifically, I want to determine whether new T is different from new T() for any template parameter T. Which of the type traits is_trivial,…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
61
votes
4 answers

What is the difference between a trait and a policy?

I have a class whose behavior I am trying to configure. template ServerTraits; Then later on I have my server object itself: template class Server {…}; My question is for my usage above…
59
votes
5 answers

Why is there not an std::is_struct type trait?

I've seen that in order to check if a type T is a class I can use: bool isClass = std::is_class::value; It returns true for both classes and structs. I know that in C++ they are almost the same thing, but I'd like to know why there's not a…
Jepessen
  • 9,377
  • 11
  • 64
  • 111
55
votes
1 answer

How does Eric Niebler's implementation of std::is_function work?

Last week Eric Niebler tweeted a very compact implementation for the std::is_function traits class: #include template struct priority_tag : priority_tag {}; template<> struct priority_tag<0> {}; // Function types…
ComicSansMS
  • 43,180
  • 11
  • 123
  • 140
49
votes
2 answers

What is decltype with two arguments?

Edit, in order to avoid confusion: decltype does not accept two arguments. See answers. The following two structs can be used to check for the existance of a member function on a type T during compile-time: // Non-templated helper struct: struct…
leemes
  • 42,229
  • 18
  • 115
  • 172
45
votes
1 answer

Disambiguate overloaded member function pointer being passed as template parameter

I am attempting to recreate the Observer pattern where I can perfectly forward parameters to a given member function of the observers. If I attempt to pass the address of a member function which has multiple overrides, it cannot deduce the correct…
Steve Lorimer
  • 22,912
  • 14
  • 99
  • 180
42
votes
1 answer

What is the purpose of C++20 std::common_reference?

C++20 introduces std::common_reference. What is its purpose? Can someone give an example of using it?
康桓瑋
  • 5,696
  • 1
  • 10
  • 33
40
votes
1 answer

C++11 is_same type trait for templates

Is it possible to check that type T is an std::array of arbitrary type and size? I can check for a particular array, for instance: is_same>::value But I'd like to check that T is any instantiation of std::array. Something…
user2052436
  • 3,309
  • 1
  • 19
  • 32
38
votes
4 answers

How do I switch/select types during compile-time?

Is there a standard way for me to select a type at compile-time on an unsigned index in c++11? For example, something like: using type_0 = static_switch<0,T,U>; // yields type T using type_1 = static_switch<1,T,U>; // yields type U If there is a…
kfmfe04
  • 14,193
  • 11
  • 67
  • 132
36
votes
2 answers

With C++17 is it possible to detect if a struct/class has any base?

I need a type trait which will be true if the given type derives from anything, and false otherwise. For example: template struct is_inherit //... logic of inheritance detection ; template void AppLogic(){ if…
Nyufu
  • 339
  • 3
  • 12
36
votes
5 answers

Difference between char and signed char in c++?

Consider the following code : #include #include int main(int argc, char* argv[]) { std::cout<<"std::is_same::value = "<::value<
Vincent
  • 50,257
  • 51
  • 171
  • 339
35
votes
5 answers

What do compilers do with compile-time branching?

EDIT: I took the "if/else" case as an example that can sometimes be resolved at compile time (eg when static values are involved, cf ). Adapting the answers below to other types of static branching (eg, multiple branches or…
Jonathan H
  • 6,750
  • 5
  • 39
  • 68
35
votes
8 answers

Find out whether a C++ object is callable

Is it possible to write a type trait, say is_callable which tells if an object has an operator() defined? It is easy if the arguments to the call operator are known in advance, but not in the general case. I want the trait to return true if and…
Antoine
  • 11,369
  • 6
  • 33
  • 47
1
2 3
68 69