public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101698] New: Template type conversion operator from base class preferred over matching overload
@ 2021-07-30 19:11 ammiera at hotmail dot com
  2021-08-02  8:28 ` [Bug c++/101698] [9/10/11/12 Regression] " rguenth at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: ammiera at hotmail dot com @ 2021-07-30 19:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101698
           Summary: Template type conversion operator from base class
                    preferred over matching overload
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ammiera at hotmail dot com
  Target Milestone: ---

There are two conversion operators, template and non-templated.
Their return type might match:

virtual operator const std::string&() const;
template <class T> operator const T&() const;

If so, when trying to call it, the template is always preferred over
non-template and there's no way to SFINAE out of it:


#include <iostream>
#include <string>
#include <type_traits>

class Base {
 public:
  template <class T>
  operator const T&() const {
    std::cout << "use template method" << std::endl;
    static T tmp{};
    return tmp;
  }

  virtual operator const std::string&() const {
    std::cout << "use overload method" << std::endl;
    const static std::string tmp;
    return tmp;
  }
};

template <class T>
class Derive : public Base {
 public:
  operator const T&() const override {
    using Y = std::string;
    static_assert(std::is_same<T, Y>::value, "");

    std::string static res;

    res = Base::operator const Y&();
    res = Base::operator const T&();
    return res;
  }
};

int main() {
  Derive<std::string> a;
  const std::string& b = a;
  (void)b;
}

Reproduced locally on:
gcc (SUSE Linux) 11.1.1 20210721 [revision
076930b9690ac3564638636f6b13bbb6bc608aea]
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Did a quick bisection via godbolt.org,  the last version it's didn't occur is
7.5.

https://gcc.godbolt.org/z/KcbK9vv5z

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

end of thread, other threads:[~2022-05-13 17:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-30 19:11 [Bug c++/101698] New: Template type conversion operator from base class preferred over matching overload ammiera at hotmail dot com
2021-08-02  8:28 ` [Bug c++/101698] [9/10/11/12 Regression] " rguenth at gcc dot gnu.org
2021-08-02  9:37 ` redi at gcc dot gnu.org
2021-08-02 10:10 ` redi at gcc dot gnu.org
2022-01-17 13:58 ` rguenth at gcc dot gnu.org
2022-04-14  0:24 ` cvs-commit at gcc dot gnu.org
2022-04-14  0:26 ` [Bug c++/101698] [9/10/11 " jason at gcc dot gnu.org
2022-05-13 16:28 ` cvs-commit at gcc dot gnu.org
2022-05-13 17:14 ` [Bug c++/101698] [9/10 " cvs-commit at gcc dot gnu.org
2022-05-13 17:41 ` [Bug c++/101698] [9 " cvs-commit at gcc dot gnu.org
2022-05-13 17:58 ` jason 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).