纯虚函数会阻止隐式生成的移动构造函数吗?
do pure virtual functions prevent implicitly generated move constructors?
#include <type_traits>
struct test {
virtual void foo() noexcept = 0;
};
struct test2 : test {
void foo() noexcept override final {}
};
// fails
static_assert(std::is_move_constructible<test>::value, "test not move constructible");
// succeeds
static_assert(std::is_move_constructible<test2>::value, "test2 not move constructible");
(Live)
根据 cppreference.com(据我所知),test
应该有一个隐式生成的移动构造函数:
The implicitly-declared or defaulted move constructor for class T is defined as deleted in any of the following is true:
- T [= test] has non-static data members that cannot be moved (have deleted, inaccessible, or ambiguous move constructors)
- T has direct or virtual base class that cannot be moved (has deleted, inaccessible, or ambiguous move constructors)
- T has direct or virtual base class with a deleted or inaccessible destructor
- T is a union and has a variant member with non-trivial copy constructor
- (until C++14) T has a non-static data member or a direct or virtual base without a move constructor that is not trivially copyable.
为什么编译器不为 test
生成隐式移动构造函数? test2
为什么要这样做?
std::is_move_constructible
检查类型是否可以从右值参数构造。 test
是抽象的 class 类型。无论参数如何,它都无法构造。
#include <type_traits>
struct test {
virtual void foo() noexcept = 0;
};
struct test2 : test {
void foo() noexcept override final {}
};
// fails
static_assert(std::is_move_constructible<test>::value, "test not move constructible");
// succeeds
static_assert(std::is_move_constructible<test2>::value, "test2 not move constructible");
(Live)
根据 cppreference.com(据我所知),test
应该有一个隐式生成的移动构造函数:
The implicitly-declared or defaulted move constructor for class T is defined as deleted in any of the following is true:
- T [= test] has non-static data members that cannot be moved (have deleted, inaccessible, or ambiguous move constructors)
- T has direct or virtual base class that cannot be moved (has deleted, inaccessible, or ambiguous move constructors)
- T has direct or virtual base class with a deleted or inaccessible destructor
- T is a union and has a variant member with non-trivial copy constructor
- (until C++14) T has a non-static data member or a direct or virtual base without a move constructor that is not trivially copyable.
为什么编译器不为 test
生成隐式移动构造函数? test2
为什么要这样做?
std::is_move_constructible
检查类型是否可以从右值参数构造。 test
是抽象的 class 类型。无论参数如何,它都无法构造。