Why does C++ think my class is copy-constructible when it can't be?
a year ago
- #Template
- #C++
- #Copy Constructor
- 断言`std::is_copy_constructible_v<Derived<int>>`通过是因为编译器检查的是非删除拷贝构造函数的存在性,而非其实例化。
- `Derived<int>`具有用户声明且未标记为删除的拷贝构造函数,因此编译器假定它是可拷贝构造的。
- 尝试拷贝`Derived<int>`会失败,因为它需要拷贝`Base<int>`——而基类的拷贝构造函数已被删除。
- 若将`Derived`的拷贝构造函数设为默认(`= default`),由于基类不可拷贝,该函数会被隐式删除,导致断言失败。
- 编译器在`is_copy_constructible`检查中仅验证拷贝构造函数的声明,不检查其实例化可行性。