I came across this post discussing class conversion functions and everything made sense except for this piece of code:
struct test {
void operator[](unsigned int) { }
operator char *() { static char c; return &c; }
};
int main() {
test t; t[0]; // ambiguous
}
// (t).operator[] (unsigned int) : member
// operator[](T *, std::ptrdiff_t) : built-in
The explaination was: The call can be ambiguous, because for the member, the second parameter needs a conversion, and for the built-in operator, the first needs a user defined conversion. The other two parameters match perfectly respectively. The call can be non-ambiguous in some cases (ptrdiff_t needs be different from int then).
I am completely lost here and don’t understand what second parameter is being talked about for the member function and why the built-in operator would be invoked at all given there exists a conversion function.
Link to the How do conversion operators work in C++?
I’ve tried running to code and didn’t notice anything out of the ordinary, though I also don’t know what I’m looking for.
Tskcool is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.