[Bug c++/106993] GCC accepts invalid program involving templated conversion function
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Sep 21 10:02:02 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106993
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced:
struct string_view { string_view(const char*) { } };
struct object{};
struct foo{
foo operator[](string_view s){
return foo{};
}
template <typename T>
operator T(){
return object{};
}
};
int main(){
foo f;
f["a"];
}
Clang and EDG both say this is ambiguous, because f["a"] could be either:
"a"[(ptrdiff_t) f]
or:
f.operator[](string_view("a"));
GCC and MSVC select the second one.
If the conversion operator isn't a template, then all compilers agree it's
ambiguous:
struct string_view { string_view(const char*) { } };
struct foo{
foo operator[](string_view s){
return foo{};
}
operator long(){
return 0;
}
};
int main(){
foo f;
f["a"];
}
More information about the Gcc-bugs
mailing list