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

            Bug ID: 88759
           Summary: `decltype(auto)` as return type of abbreviated
                    function template strips cv-qualifications and
                    referenceness
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ensadc at mailnesia dot com
  Target Milestone: ---

https://wandbox.org/permlink/hPwFtdVsAd4kIu44

(-fconcepts is required to silent the warning "use of 'auto' in parameter
declaration only available with -fconcepts", but otherwise the default setting
is enough to trigger the error.)
====
decltype(auto) f(auto x) {
    return x[0];
}

const int x[] = {0};

extern decltype(f(x)) a;
extern const int& a;

====
prog.cc:8:19: error: conflicting declaration 'const int& a'
    8 | extern const int& a;
      |                   ^
prog.cc:7:23: note: previous declaration as 'int a'
    7 | extern decltype(f(x)) a;
      |                       ^
====
Originally discovered in
https://stackoverflow.com/questions/54062172/understanding-decltypeauto-difference-in-case-of-template-vs-auto-functions.
I rewrote the code to better illustrate the issue.

`decltype(f(x))` should be `const int&`, but GCC thinks it's `int`.

It seems that `decltype(auto)` is treated the same as `auto` when used as
return type of abbreviated function template.

Changing the abbreviated function template to a regular function template (i.e.
changing `decltype(auto) f(auto x)` to `template<class T> decltype(auto) f(T
x)`) removes the error.

Probably related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69448.

Reply via email to