Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking address of rvalue

Why does GCC version >= 10.1 produces a taking address of rvalue error with the following code? Or, when using an lvalue instead, why does it spit out a no matching function for call to 'a::n(a*)' error? Where does the pointer parameter come from? Clang seems perfectly fine with accepting the code (and older GCC versions as well). Compiling with -std=c++14 or -std=c++17 results in the same behavior.

The example code:

struct a {
    constexpr auto n() const { return 3; }
    //
    // static constexpr auto n() { return 3; }    // ok
    // auto n() const { return 3; }               // ok
};

template<typename>
constexpr auto f() {
    int{a{}.n()};      // error
    //
    int(a{}.n());      // ok
    int x{a{}.n()};    // ok
}

constexpr auto g() {
    int{a{}.n()};      // ok
    int(a{}.n());      // ok
    int x{a{}.n()};    // ok
}

The error that is produced:

<source>: In function 'constexpr auto f()':
<source>:10:14: error: taking address of rvalue [-fpermissive]
   10 |     int{a{}.n()};      // error
      |         ~~~~~^~
<source>:10:14: error: no matching function for call to 'a::n(a*)'
<source>:2:20: note: candidate: 'constexpr auto a::n() const'
    2 |     constexpr auto n() const { return 3; }
      |                    ^
<source>:2:20: note:   candidate expects 0 arguments, 1 provided
like image 244
303 Avatar asked Dec 19 '25 08:12

303


1 Answers

As shown in the comment and gcc bugtracker (Bugreport on gcc) this is a compiler bug and has been fixed in gcc 12.

like image 62
Goswin von Brederlow Avatar answered Dec 21 '25 22:12

Goswin von Brederlow