非常抱歉标题表述不够明确(由于我的英语水平有限)。请建议一个更好的标题。
请考虑以下代码。
struct A {
typedef std::vector<double> State;
// template <class... Args>
// A(Args... args)
// : a(args...)
// {}
template <class... Args>
A(Args&&... args)
: a(std::forward<Args>(args)...)
{}
A(const A&) = default;
A(A&&) = default;
State a;
};
int main(){
A a(3,2);
A b = a; // This line triggers an error!!
}
Gcc 4.8.0编译出现错误,错误信息为error: no matching function for call to 'std::vector<double>::vector(A&)' : a(std::forward<Args>(args)...)
.
我不明白为什么这段代码是错误的。在我看来,在A b = a;
这一行中,编译器应该调用拷贝构造函数。
然而,如果我将构造函数替换为注释掉的那个(它只是简单地接受值),那么它可以编译。此外,现在默认的拷贝(和移动)构造函数的行也不再需要了。这里发生了什么?
A(A&) = default
,它可以编译通过。 :) - Sungmin