考虑以下两个例子:
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 {}
};和用法:
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;产出如下:
1
1
0
1编译器使用: GCC 4.8.1。
因此,如果我显式地编写默认B构造函数,(X)会生成1;另一方面,如果由于继承而可用默认C构造函数,则(Y)会生成0。为什么会这样呢?
is_nothrow_constructible 是否意味着在使用特性时没有考虑到继承的构造函数?
发布于 2016-12-20 09:56:25
这里的问题是模板化构造函数隐藏了继承的构造函数。第12.9/4节:
如此声明的构造函数..。如果删除X中的相应构造函数(8.4.3),或者如果默认的默认构造函数(12.1)被删除,则删除.
以下编译是没有问题的:
struct C: A {
using A::A;
};
static_assert(std::is_nothrow_constructible<C>{}, "");https://stackoverflow.com/questions/41239062
复制相似问题