为什么没有调用复制构造函数
Why copy constructor is not invoked?
抱歉,标题过于模糊。(因为我的英语水平不高)。请建议一个更好的标题。
请考虑下面的代码。
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;
行中的复制构造函数。
但是,如果我将构造函数替换为注释过的构造函数(它只接受值)。它确实可以编译。此外,现在不需要用于默认复制(和移动)构造函数的行。这里发生了什么?
在c++ 11中,让编译器自动推断模板参数(就像你必须用模板化的构造函数那样),并将&&
应用于类型,创建一个通用引用,它匹配任何具有任何cv资格的类型,无论是左值引用还是右值引用。
所以在你的情况下,你在传递一个A
,因此Args...
= A &
, Args &&...
= A & &&
,这是A &
多亏了引用崩溃规则,这是一个比const A &
更好的匹配,因为编译器不需要添加constness到一个非const变量。
我认为在这种情况下模板构造函数是一个更好的匹配,因为它接受非const值。如果你把a
改成const
,它会调用复制构造函数…
const A a(3,2);
A b = a;
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 复制构造函数、赋值运算符C++
- std::ofstream 作为类成员删除复制构造函数?
- 复制构造函数C++无法正确复制指针
- 关于复制构造函数的一个棘手问题
- 为什么调用复制构造函数而不是移动构造函数?
- 填充上编译器生成的复制构造函数之间的不一致
- C++ 对象指针数组的复制构造函数
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 防止在复制构造函数中隐式调用基构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 具有已删除移动和复制构造函数的类的就地构造
- 复制构造函数隐式转换问题
- 复制构造函数中的递归调用