303
303

Reputation: 4732

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

Upvotes: 17

Views: 1835

Answers (1)

Goswin von Brederlow
Goswin von Brederlow

Reputation: 12332

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

Upvotes: 1

Related Questions