Std :: is_default_constructible <T> error if the constructor is private

I have the following snippet

#include <type_traits> #include <boost/type_traits.hpp> class C { C() { } }; int main() { static_assert(!boost::has_trivial_default_constructor<C>::value, "Constructible"); static_assert(!std::is_default_constructible<C>::value, "Constructible"); } 

The conditions are not equal, but the first condition works fine, and the second construction gives an error that the constructor is private. The gcc 4.7 compiler ... So, is this a gcc bug, or is it defined as standard?

http://liveworkspace.org/code/NDQyMD $ 5

OK Since these conditions are really unequal - we can use something like this

 #include <type_traits> #include <boost/type_traits.hpp> class C { private: C() noexcept(false) { } }; int main() { static_assert(!boost::has_nothrow_constructor<C>::value, "Constructible"); static_assert(!std::is_nothrow_constructible<C>::value, "Constructible"); } 

http://liveworkspace.org/code/NDQyMD $ 24

In any case, I know that static_assert should not fail, since the types are really not constructive by default / not constructive. Question: WHY is there a compilation error and not my static assert?

+8
c ++ c ++ 11 typetraits
source share
1 answer

Looks like a compiler error. Let's look at the following SFINAE example (a similar implementation is used in the g++ type_traits )

 #include <type_traits> class Private { Private() { } }; class Delete { Delete() = delete; }; struct is_default_constructible_impl { template<typename T, typename = decltype(T())> static std::true_type test(int); template<typename> static std::false_type test(...); }; template <class T> struct is_default_constructible: decltype(is_default_constructible_impl::test<T>(0)) { }; int main() { static_assert(is_default_constructible<Private>::value, "Private is not constructible"); static_assert(is_default_constructible<Delete>::value, "Delete is not constructible"); } 

The second statement works as expected, we cannot deduce the Delete type, since the class has only one remote constructor. But when the compiler tries to infer the type Private() , it gives an error: Private::Private() is private . So, we have two classes with a private constructor, but one of them gives an error, and the second does not. I think that this is the wrong behavior, but I can not find confirmation in the standard.

PS all provided codes are compiled by clang without any errors.

+5
source share

All Articles