我想在模板函数中创建一些typename Type的本地对象:
template <typename Type, typename... Args>
void create_local(Args... args)
{
Type val(args...);
}
现在,当我调用此函数时没有参数(其中Type是一个具有不可复制成员的类):
struct T {
std::mutex m;
};
int main()
{
T t; // OK: No use of move constructor
create_local<T>(); // Error: Requires deleted move constructor!!!
}
(coliru link)
g(从4.7.3到5.2)无法编译,需要定义T的移动构造函数? clang 3.7编译成功.
另外,如果我(1)从T中删除std :: mutex成员,(2)声明T的默认构造函数,(3)为T声明一个已删除的复制构造函数:
struct T {
T() = default;
T(const T&) = delete;
};
int main()
{
T t; // OK: No use of move constructor
create_local<T>(); // OK: No use of move constructor
}
所有版本的g和clang编译成功.为什么g不能为任何类型编译具有不可复制成员的类型?
最佳答案 根据Andrey Zholos在 this错误报告中的评论:
I also stumbled onto this bug, and bug 59141 is a duplicate.
It appears the empty parameter pack is expanded as t({}) rather than t().
There is a similar example in 14.5.3p6 that indicates obj should be value-initialized (not copy-constructed), and clang accepts this code.