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

* [Bug c++/101698] [9/10/11/12 Regression] Template type conversion operator from base class preferred over matching overload
  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 ` rguenth at gcc dot gnu.org
  2021-08-02  9:37 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-08-02  8:28 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
            Summary|Template type conversion    |[9/10/11/12 Regression]
                   |operator from base class    |Template type conversion
                   |preferred over matching     |operator from base class
                   |overload                    |preferred over matching
                   |                            |overload
      Known to work|                            |7.5.0
   Target Milestone|---                         |9.5

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

* [Bug c++/101698] [9/10/11/12 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-02  9:37 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |10.3.0, 11.2.0, 12.0,
                   |                            |8.1.0, 9.4.0
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-08-02

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Bisection shows that r255598 aka r8-5266 was OK.

r255605 started to ICE. r256986 fixed the ICE, but the wrong function was
overridden.

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

* [Bug c++/101698] [9/10/11/12 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-02 10:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The overriding behaviour changed with r255605, it's just that the assertion
caused an ICE until it was removed.

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

* [Bug c++/101698] [9/10/11/12 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (2 preceding siblings ...)
  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
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-17 13:58 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

* [Bug c++/101698] [9/10/11/12 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (3 preceding siblings ...)
  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
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-04-14  0:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:d4e00ccef6c706a4a4a6446bffaf4111f98d5771

commit r12-8151-gd4e00ccef6c706a4a4a6446bffaf4111f98d5771
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Apr 13 14:49:04 2022 -0400

    c++: template conversion op [PR101698]

    Asking for conversion to a dependent type also makes a BASELINK dependent.

            PR c++/101698

    gcc/cp/ChangeLog:

            * pt.cc (tsubst_baselink): Also check dependent optype.

    gcc/testsuite/ChangeLog:

            * g++.dg/template/conv19.C: New test.

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

* [Bug c++/101698] [9/10/11 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (4 preceding siblings ...)
  2022-04-14  0:24 ` cvs-commit at gcc dot gnu.org
@ 2022-04-14  0:26 ` jason at gcc dot gnu.org
  2022-05-13 16:28 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jason at gcc dot gnu.org @ 2022-04-14  0:26 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |12.0
           Assignee|unassigned at gcc dot gnu.org      |jason at gcc dot gnu.org
                 CC|                            |jason at gcc dot gnu.org
      Known to fail|12.0                        |
             Status|NEW                         |ASSIGNED
            Summary|[9/10/11/12 Regression]     |[9/10/11 Regression]
                   |Template type conversion    |Template type conversion
                   |operator from base class    |operator from base class
                   |preferred over matching     |preferred over matching
                   |overload                    |overload

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

* [Bug c++/101698] [9/10/11 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (5 preceding siblings ...)
  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
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-13 16:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jason Merrill
<jason@gcc.gnu.org>:

https://gcc.gnu.org/g:5c6577dcf1d413582775063c905d917e21bf0eba

commit r11-9994-g5c6577dcf1d413582775063c905d917e21bf0eba
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Apr 13 14:49:04 2022 -0400

    c++: template conversion op [PR101698]

    Asking for conversion to a dependent type also makes a BASELINK dependent.

            PR c++/101698

    gcc/cp/ChangeLog:

            * pt.c (tsubst_baselink): Also check dependent optype.

    gcc/testsuite/ChangeLog:

            * g++.dg/template/conv19.C: New test.

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

* [Bug c++/101698] [9/10 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (6 preceding siblings ...)
  2022-05-13 16:28 ` cvs-commit at gcc dot gnu.org
@ 2022-05-13 17:14 ` 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
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-13 17:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jason Merrill
<jason@gcc.gnu.org>:

https://gcc.gnu.org/g:f06e1ff97cb383118afed4c87dfa104e7c4b141d

commit r10-10737-gf06e1ff97cb383118afed4c87dfa104e7c4b141d
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Apr 13 14:49:04 2022 -0400

    c++: template conversion op [PR101698]

    Asking for conversion to a dependent type also makes a BASELINK dependent.

            PR c++/101698

    gcc/cp/ChangeLog:

            * pt.c (tsubst_baselink): Also check dependent optype.

    gcc/testsuite/ChangeLog:

            * g++.dg/template/conv19.C: New test.

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

* [Bug c++/101698] [9 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (7 preceding siblings ...)
  2022-05-13 17:14 ` [Bug c++/101698] [9/10 " cvs-commit at gcc dot gnu.org
@ 2022-05-13 17:41 ` cvs-commit at gcc dot gnu.org
  2022-05-13 17:58 ` jason at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-13 17:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jason Merrill
<jason@gcc.gnu.org>:

https://gcc.gnu.org/g:de0b78d1e75e9483b4550abc38b9199c96465dc5

commit r9-10174-gde0b78d1e75e9483b4550abc38b9199c96465dc5
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Apr 13 14:49:04 2022 -0400

    c++: template conversion op [PR101698]

    Asking for conversion to a dependent type also makes a BASELINK dependent.

            PR c++/101698

    gcc/cp/ChangeLog:

            * pt.c (tsubst_baselink): Also check dependent optype.

    gcc/testsuite/ChangeLog:

            * g++.dg/template/conv19.C: New test.

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

* [Bug c++/101698] [9 Regression] Template type conversion operator from base class preferred over matching overload
  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
                   ` (8 preceding siblings ...)
  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
  9 siblings, 0 replies; 11+ messages in thread
From: jason at gcc dot gnu.org @ 2022-05-13 17:58 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> ---
Fixed for 9.5/10.4/11.4/12.

^ 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).