0 cannot be a string , but it can be a pointer, which means that it can be implicitly converted to string . Please note that this does not apply to other constant integrals, for example, 1 or 42 - only 0 .
4.10 Convert Pointers
1 / The null pointer constant is the integral constant expression (5.19) of the rvalue of integer type, which is zero. The null pointer constant can be converted to a pointer type; the result is a null pointer to a value of this type, and a pointer to an object or a pointer to a function type is different from any other value. Two null pointer values ββof the same type must be compared equal. The conversion of a null pointer constant to a pointer to a cv-qualified type is one conversion and not a sequence of pointer conversion with subsequent qualification conversion (4.4).
So, in the case of myclass[0] , 0 can be either an int constant or a `null pointer.
The standard also states that std::string has a non- explicit constructor that accepts a char pointer:
size_type find_last_not_of (const charT* s, size_type pos = npos) const;
Now, since your operator& methods take parameters of a const reference type, they can be passed temporary. This is why the compiler is confused - it does not know which one you want - one that accepts int , or one that takes up a temporary string constructed with string(const char*) .
As for the solution to this problem, I would take a step back. It seems to me that your two operator[] functions do completely different things. Or maybe they do the same thing, given the different inputs. If they do different things, I would provide member functions that have different (corresponding) names, and skip trying to use the operator[] syntax. Perhaps one of these methods returns what is actually indexed - in this case, I would use the operator[] syntax for this, but only that.
If they really do the same thing, and that the point is to return the element by index, I would provide only one method for this and would require size_t by value. Then you can also provide some conversion function (preferably as a free, non-member function) that converts, say, string to size_t . Having done this, you can write your code, for example, when indexing a string :
myPos[str_to_index(str)];