public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106993] New: GCC accepts invalid program involving templated conversion function
@ 2022-09-21  7:43 jlame646 at gmail dot com
  2022-09-21 10:02 ` [Bug c++/106993] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: jlame646 at gmail dot com @ 2022-09-21  7:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106993

            Bug ID: 106993
           Summary: GCC accepts invalid program involving templated
                    conversion function
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jlame646 at gmail dot com
  Target Milestone: ---

The following invalid(afaik) program is accepted by gcc and msvc. Demo link:
https://godbolt.org/z/E5crEqqfx

```
#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"]; 
}
```

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/106993] GCC accepts invalid program involving templated conversion function
  2022-09-21  7:43 [Bug c++/106993] New: GCC accepts invalid program involving templated conversion function jlame646 at gmail dot com
@ 2022-09-21 10:02 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2022-09-21 10:02 UTC (permalink / raw)
  To: gcc-bugs

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"]; 
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-21 10:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-21  7:43 [Bug c++/106993] New: GCC accepts invalid program involving templated conversion function jlame646 at gmail dot com
2022-09-21 10:02 ` [Bug c++/106993] " redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).