Could you please explain why I'm having error: call of overloaded ‘func(const Test&)’ is ambiguous
despite the fact that I use explicit template instantiation?
#include <iostream>
struct Test {
};
void func(const Test &) {
std::cout << "By Reference" << std::endl;
}
void func(const Test) {
std::cout << "By Value" << std::endl;
}
template <typename... TArgs>
void wrap(TArgs... args) {
func(args...);
}
int main() {
Test t;
wrap<const Test &>(t);
return 0;
};
EDIT
The reason of ambiguity is a combination of two factors. The first is that simple overload rules applied in the call func(args...)
. The second is that simple functions cannot be overloaded by value and const reference. To ensure, one may replace the call wrap<const Test &>(t)
with func(static_cast<const Test &>(t))
. The error will still be there.
To solve the problem, one may use function template for func
and value vs const reference template specialization as showed in the example provided by @lubgr
Thanks everybody to help me to demystify the concept.
For the same reason the following calls are ambiguous:
#include <iostream>
void foo(int) { std::cout << "val" << std::endl; }
void foo(const int&) { std::cout << "ref" << std::endl; }
int main()
{
int i = 1;
foo(i);
}
See here for full discussion of that case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With