为以下C ++代码段:
#include <type_traits>
#include <iostream>
template <typename T>
struct Bar
{
Bar() = default;
Bar(const Bar&) = delete;
};
template <typename T>
struct Foo : public Bar<T>
{
Foo() = default;
Foo(const Foo& other) : Bar<T>(other) {}
};
int main() {
std::cout << std::is_copy_constructible_v<Bar<int>> << '\n'; // prints 0
std::cout << std::is_copy_constructible_v<Foo<int>> << '\n'; // prints 1
return 0;
}
,有人知道为什么
Foo
显然不是抄本构造吗?
换句话说,为什么删除的copy构造函数是不通过Bar
?繁殖的 标准确实说:“只有变量定义的直接上下文的有效性被视为”,但我不明白为什么他们会决定这样定义它。最终,这是否意味着,如果没有明确的检查,绝对不可能发现
Foo
不仅可以单独检查
std::is_constructible
,无论有没有修改?
这是一个有趣的案例,因为如果您实际尝试使用Foo
的复制ctor,则会发现它不会编译,因为它试图使用
Bar
的删除副本ctor.
I猜测
Foo
不检查定义的方法是否编译,并且您在定义上没有收到编译错误,因为在实例化特定函数之前,没有完全检查模板。如果您删除模板或制作
Bar
Bar
,那么它既汇编又是构建性的,因为您明确地覆盖了默认值。