Std :: is_nothrow_constructible on constructor inheritance

Let's look at two examples:

struct A { A () noexcept = default; }; struct B : A { B () noexcept = default; template <typename T> B (T) noexcept {} }; struct C : A { using A::A; template <typename T> C (T) noexcept {} }; 

and use:

 std::cout << std::is_nothrow_constructible<B>::value << std::endl; // (X) std::cout << std::is_nothrow_constructible<B, int>::value << std::endl; std::cout << std::is_nothrow_constructible<C>::value << std::endl; // (Y) std::cout << std::is_nothrow_constructible<C, int>::value << std::endl; 

Output:

 1 1 0 1 

The compiler is used: GCC 4.8.1.

So, if I write explicitly the default constructor B , (X) creates 1, on the other hand, if the default constructor C is available due to inheritance, (Y) produces 0. Why is this?

Does this mean that inherited constructors are not taken into account when using the is_nothrow_constructible attribute?

+5
source share
1 answer

The problem is that the template constructor hides the inherited constructor. From section: 12.9 / 4:

Constructor declared as [...]. It is deleted if the corresponding constructor in X is removed (8.4.3), or if the default default constructor (12.1) is deleted, [...].

The following compilations are no problem:

 struct C: A { using A::A; }; static_assert(std::is_nothrow_constructible<C>{}, ""); 
+6
source

Source: https://habr.com/ru/post/1261559/


All Articles