在下面的代码中,默认构造函数以及带有int参数的参数化构造函数正在创建临时对象。但是,字符串向量作为构造函数的参数甚至没有编译。有人可以解释这种行为吗?
#include <vector>
#include <string>
class C {
public:
std::vector<std::string> v;
int i;
C() {
}
C(int j) {
i = j;
}
C(std::vector<std::string> tmp) {
v = tmp;
}
};
int main() {
std::vector<std::string> v = {"Stack", "Overflow"};
C(); // works..
C(1); // works..
C(v); // compilation fails..
return 0;
}
下面是编译输出:
g++ -g constructor.cpp -std=c++1z
constructor.cpp: In function ‘int main()’:
constructor.cpp:25:8: error: conflicting declaration ‘C v’
C(v); // compilation fails..
^
constructor.cpp:22:27: note: previous declaration as ‘std::vector<std::__cxx11::basic_string<char> > v’
std::vector<std::string> v = {"Stack", "Overflow"};
^