以下是简化版的代码:
#include <string>
#include <string_view>
struct object{
operator std::string(){return "";}
}
struct foo{
foo operator[](std::string_view s){
return foo{};
}
template <typename T>
operator T(){
return object{};
}
};
int main(){
foo f;
std::string s = f["a"];
}
clang 报错:
error: use of overloaded oeprator '[]' is ambiguous (with oeprand types 'foo' and 'const char*')
note: candidate function foo operator[](std::string_view s)
note: built-in candidate operator[](long, const char*)
note: built-in candidate operator[](long, const volatile char*)
但gcc成功编译了上述代码。
clang版本为12.0.1,gcc版本为7.5.0。
我很困惑,哪个编译器是正确的?
"foo"[3] == 3["foo"]
。 - Patrick Roberts