小编cap*_*500的帖子

为什么从不可移动类派生的类本身是可移动构造的?

根据 cppreference,从不可移动的类派生也应该使派生类不可移动。那么为什么std::is_move_constructible_v派生类会返回true呢?

class NonMovable{
    public:
        NonMovable(const NonMovable&) = default;
        NonMovable(NonMovable&&) = delete;
        
        NonMovable& operator = (const NonMovable&) = default;
        NonMovable& operator = (NonMovable&&) = delete;
        
        NonMovable() = default;
};

class Derived : public NonMovable{};

int main(){
    std::cout << std::is_move_constructible_v<NonMovable> << "\n"; // 0
    std::cout << std::is_move_constructible_v<Derived> << "\n"; // 1
}
Run Code Online (Sandbox Code Playgroud)

c++ type-traits move-semantics c++11

6
推荐指数
1
解决办法
72
查看次数

标签 统计

c++ ×1

c++11 ×1

move-semantics ×1

type-traits ×1