为什么编译器在不需要复制构造函数时会关心它?
#include <iostream>
template<typename T>
void print(T);
class Foo {
Foo(const Foo&);
public:
Foo(){}
};
template<>
void print(const Foo& f) {
std::cout << "Foo\n";
}
int main(){
Foo foo;
print(foo);
}
函数print
被重载以接受const Foo&
但编译器产生以下编译错误:
main.cpp: In function ‘int main()’:
main.cpp:21:14: error: ‘Foo::Foo(const Foo&)’ is private within this context
21 | print(foo);
| ^
main.cpp:7:5: note: declared private here
7 | Foo(const Foo&);
| ^~~
main.cpp:4:12: note: initializing argument 1 of ‘void print(T) [with T = Foo]’
4 | void print(T);
| ^
为什么会这样?显然我们不需要复制构造函数,因为我们通过foo
引用传递并且我们有重载print
。