public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100720] New: inconsistent return type deduction behavior with user defined conversion function
@ 2021-05-21 20:40 nickgray0 at brown dot edu
  2021-08-07  1:46 ` [Bug c++/100720] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: nickgray0 at brown dot edu @ 2021-05-21 20:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100720
           Summary: inconsistent return type deduction behavior with user
                    defined conversion function
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nickgray0 at brown dot edu
  Target Milestone: ---

the following code compiles successfully:

    auto f(auto);

    template<typename = int>
    struct A {
        auto g() {
            return f(0);
        }
    };

    auto f(auto) {
        if (true)
            return 42;
        else
            return A{}.g();
    }

    auto main()->int {
        f(123);
    }

however if we rename A::g as a conversion function:

    auto f(auto);

    template<typename = int>
    struct A {
        operator auto() {
            return f(0);
        }
    };

    auto f(auto) {
        if (true)
            return 42;
        else
            return static_cast<int>(A{});
    }

    auto main()->int {
        f(123);
    }

it fails with the error: <source>: In instantiation of 'A<
<template-parameter-1-1> >::operator auto() [with <template-parameter-1-1> =
int]':
<source>:14:36:   required from here
<source>:6:17: error: use of 'auto f(auto:1) [with auto:1 = int]' before
deduction of 'auto'
    6 |         return f(0);
      |                ~^~~
<source>: In function 'auto f(auto:1)':
<source>:14:16: error: invalid 'static_cast' from type 'A<int>' to type 'int'
   14 |         return static_cast<int>(A{});
      |                ^~~~~~~~~~~~~~~~~~~~~
ASM generation compiler returned: 1

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

* [Bug c++/100720] inconsistent return type deduction behavior with user defined conversion function
  2021-05-21 20:40 [Bug c++/100720] New: inconsistent return type deduction behavior with user defined conversion function nickgray0 at brown dot edu
@ 2021-08-07  1:46 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-07  1:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
clang rejects the first example:
<source>:7:20: error: function 'f<int>' with deduced return type cannot be used
before it is defined
            return f(0);
                   ^
<source>:2:10: note: 'f<int>' declared here
    auto f(auto);
         ^

And rejects the second with a similar message as GCC.

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

end of thread, other threads:[~2021-08-07  1:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-21 20:40 [Bug c++/100720] New: inconsistent return type deduction behavior with user defined conversion function nickgray0 at brown dot edu
2021-08-07  1:46 ` [Bug c++/100720] " pinskia 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).