Through some googling, I figured out the 'solution' to my problem, but I'm not understanding why there was a problem in the first place, or why this fixes it. I found the answer through a foreign language forum, and google translate didn't do a perfect job in helping me understand what was being said.
If you have a class like this:
class MyClass
{
public:
MyClass(){ }
MyClass &operator [](std::string string) {return *this;}
operator int() {return 0;}
};
And try to compile this code:
MyClass myClass;
myClass["test"];
It gives this compile-time error message:
[color=#aa0000]error: ambiguous overload for 'operator[]' in 'myClass["test"]'
[color=#aa0000]main.cpp:11: note: candidates are: operator[](int, const char*) <built-in>
[color=#aa0000]MyClass.h:5: note: MyClass& MyClass::operator[](std::string)
But works fine if you change the class to this:
class MyClass
{
public:
MyClass(){ }
MyClass &operator [](const char*) {return *this;} //const char* not std::string.
operator int() {return 0;}
};
Why is an operator[](int, const char*) "<built-in>" to my class? What for?
And why, if given the option between:
operator[](int, const char*) and operator[](std::string)
does it decide it's too 'ambiguous', when std::string can be constructed from const char*, and operator[](int, const char*) takes an int as the first parameter?!
The weird thing, which especially confuses me, is why this error only occurs if I'm also overloading the typecast operator int or typecast operator bool, but not if I overload, say, typecast operator float.
I'm guessing it's because bool can be implicitly cast to int, and it somehow thinks I'm trying to do something like this: myClass[(int)myClass]
If I called this 'built in' operator[int, const char*] manually, what would happen? What is it there for?
My code currently works (by providing both (const char*) and (std::string) overloads for operator[]), but I'd like to understand why there was a problem in the first place.