Suppose I define an implicit conversion function to myself:
#include <iostream>
class Foo {
public:
operator Foo() {
std::cout << "wha??\n";
return Foo();
}
};
void f(Foo f) {}
int main() {
Foo foo;
f(foo);
}
Why would I define this? Well I would never write it directly, but it could happen through a template instantiation in a template class I'm writing. If this happens, I hope that defining the conversion function is effectively a no-op and that it is impossible to actually call.
The above program prints nothing (which is great). Is there any circumstance under which this conversion function will actually get called?
It's correct, but will never called, due n3376 12.3.2/1
A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it)
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