这个例子似乎用VC10和gcc编译(虽然我的gcc版本很老).
编辑:R.Martinho Fernandez在gcc 4.7上试过这个并且行为仍然是一样的.
struct Base
{
operator double() const { return 0.0; }
};
struct foo
{
foo(const char* c) {}
};
struct Something : public Base
{
void operator[](const foo& f) {}
};
int main()
{
Something d;
d["32"];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但克朗抱怨道:
test4.cpp:19:6: error: use of overloaded operator '[]' is ambiguous (with operand types 'Something' and 'const char [3]')
d["32"]
~^~~~~
test4.cpp:13:10: note: candidate function
void operator[](const foo& f) {}
^
test4.cpp:19:6: note: built-in candidate operator[](long, const …Run Code Online (Sandbox Code Playgroud) c++ clang visual-c++ overload-resolution implicit-conversion