【发布时间】:2017-01-01 12:29:12
【问题描述】:
考虑以下示例:
#include <iostream>
#include <string>
#include <utility>
template <typename Base> struct Foo : public Base {
using Base::Base;
};
struct Bar {
Bar(const Bar&) { }
Bar(Bar&&) = delete;
};
int main() {
std::cout << std::is_move_constructible<Bar>::value << std::endl; // NO
std::cout << std::is_move_constructible<Foo<Bar>>::value << std::endl; // YES. Why?!
}
为什么编译器生成一个移动构造函数,尽管基类是不可移动构造的?
这是标准还是编译器错误?是否可以“完美地传播”将构造从基类移动到派生类?
【问题讨论】:
-
好吧,除非你坚持编写可复制但不可移动的类,否则你不会遇到这个问题,而且没有理由这样做。
-
这个问题背后的想法是创建非侵入式包装类的可能性,它表现出与它们的基础完全相同的外部行为。附言很难选择接受哪个答案(我不能同时接受,对吧?):)
-
@Dmitry:不,您不能接受多个,但您应该对任何有助于您理解问题的合理尝试投赞成票。顺便说一句,很好的问题暴露了不直观的行为!
-
@Brian:
class FamousPaintingInTheLouvre? -
@einpoklum 你不会删除那种类的移动构造函数,你只会让右值的构造使用复制构造函数。
标签: c++ c++11 language-lawyer move-semantics move-constructor