永远不会调用具有默认参数的复制构造函数

Dav*_*Lin 7 c++ llvm clang copy-constructor

为什么以下程序的输出只是int3和不int3&4

#include <iostream>

class B
{
public:
    explicit B(int i) { std::cout<<"int"<<i; }
    B(const B& rhs, int i = 0) { std::cout<<"&"<<i; }
};

int main(int, char**)
{
    B b(B(3), 4);
}
Run Code Online (Sandbox Code Playgroud)

命令: clang++ test.cpp -O0

编译器:Apple clang 3.0版(标签/ Apple/clang-211.12)(基于LLVM 3.0svn)

Ric*_*ith 2

这是clang 中的一个错误,现已修复。复制省略被错误地应用于构造函数调用,因为 clang 在断定它是复制构造之前没有检查提供了多少参数。

该修复将在即将发布的 clang 3.1 版本中进行。