【发布时间】:2015-11-20 07:34:45
【问题描述】:
在本次测试中:
#include <string>
struct thing {
std::string name_;
};
class test {
thing id_;
public:
test(thing id) : id_{std::move(id)} {}
};
我希望 struct thing 有一个隐式的移动构造函数,以便 class test 可以使用 std::move() 来初始化它的数据成员。
Clang 版本 3.4.1 给出了这个错误:
error: no viable conversion from 'typename remove_reference<thing&>::type' (aka 'thing') to 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
问题可以通过在struct thing中添加移动构造函数来解决,当然也需要添加转换构造函数和显式默认的复制构造函数。
我不明白为什么我不能隐式移动 struct thing。
【问题讨论】:
-
你用的是什么编译器? ideone.com 编译得很好。
thing肯定有隐式移动构造函数,但你的编译器将id_{std::move(id)}解释为聚合初始化。 -
id_(std::move(id))(用括号代替花括号)有效。等待language-lawyer 澄清为什么聚合初始化会覆盖移动构造,或者它是否是一个错误:) -
@Quentin 我有一个聚合初始化盲点。我看不到它。谢谢,现在一切正常。
-
让another Richard Smith(Clang 的主要作者之一)问 Clang 相关问题真是令人困惑:)
标签: c++11 language-lawyer