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

            Bug ID: 116379
           Summary: Type deduction error on decltype(auto)
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 1963096826 at qq dot com
  Target Milestone: ---

GCC type deduction incorrect in special circumstances. This problem persisted
until the gcc(trunk) version on [Goldbolt](https://gcc.godbolt.org/)  
The following simple code cannot be compiled in gcc.
(see on goldbot: https://gcc.godbolt.org/z/c81fra83z)
```cpp
#include <type_traits>
#include <utility>

template<typename T>
struct X{
    T val;
    decltype(auto) value(){
        return (val);
    }
};

int main(){
    int i = 0;
    X<int&&> x{ std::move(i) };
    static_assert(std::is_same_v<decltype(x.value()), int&>);
}
```
gcc output:
```
<source>: In instantiation of 'decltype(auto) X<T>::value() [with T = int&&]':
<source>:15:50:   required from here
   15 |     static_assert(std::is_same_v<decltype(x.value()), int&>);
      |                                           ~~~~~~~^~
<source>:8:20: error: cannot bind rvalue reference of type 'int&&' to lvalue of
type 'int'
    8 |         return (val);
      |                    ^
<source>: In function 'int main()':
<source>:15:24: error: static assertion failed
   15 |     static_assert(std::is_same_v<decltype(x.value()), int&>);
      |                   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
Here decltype (auto) should have been deduced as int&, but it was incorrectly
deduced as int&&. 
Details on how to trigger this bug:
1.If the type of `val` is changed from `T` to `T&&`(There should be no
difference between the two here, both are int&&), this bug will not be
triggered.
2.If the type of `val` is not a template parameter, this bug will not be
triggered.
3.If change `return (val)` to `return (this->val)`,this bug will not be
triggered.

Reply via email to