How does has_trivial_default_constructor work?

Can someone explain to me how has_trivial_default_constructor works? I tried to find it in acceleration, but, unfortunately, there are too many macros, and I just got lost ...

How can someone detect trivial_default_constructor in C ++ using templates?

I need an example in C ++ 03, not 11.

 #include <boost/type_traits.hpp> #include <boost/static_assert.hpp> struct A{ A(){} int a; //std::vector< int > b; }; int main(int argc, char* argv[]) { struct B{ std::vector< int > b; }; bool result = boost::has_trivial_default_constructor<A>::value; //std::forward(&test); return 0; } 
+7
c ++ metaprogramming c ++ 03
source share
1 answer

Actually this is not possible (in pure C ++).

Detecting whether the type has a default constructor in SFINAE because it only includes an interface, but detects if implementation inclusion is trivial.

Therefore, the compiler must provide a specific property for this. You can find the list of built-in here , note that some of them are provided, because they require compiler intervention, while others can only be provided in order to have a single set or optimize the implementation of the standard library.

Most often you are looking for either __has_trivial_constructor , which is also supported by gcc and MSVC according to the comments or __is_trivially_constructible (specific to Clang). I must admit that I'm a little unsure of the former (what if the type has several constructors?), The latter can be used as:

 template <typename T> struct has_trivial_default_constructor { static bool const value = __is_trivially_constructible(T); }; 
+3
source share

All Articles