Why compiler cares about copy constructor when it doesn't need one?
#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);
}
function print
is overloaded to accept const Foo&
but compiler produces the following compilation error:
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);
| ^
Why is it so? Obviously we don't need copy constructor, since we passing foo
by reference and we have overload of print
for that.
template<> void print(const Foo&)
is not an overload, it's a specialization. – Pronouncetemplate<>
) don't participate in overload resolution. – Pronouncetemplate<>
. – Notice