Skip to content
Latchkey

gcc/clang "taking address of temporary" in CI

A temporary lives only until the end of the full expression. Taking its address yields a pointer to memory that is about to be destroyed, so the compiler refuses the & on an rvalue.

What this error means

Applying & to a function return value, a literal, or a constructed temporary fails with "taking address of temporary".

gcc
app.cpp:6:16: error: taking address of temporary [-fpermissive]
    6 |   int* p = &make();
      |                ^~~~~~

Common causes

How to fix it

Bind to a named object first

  1. Store the temporary in a named variable, then take its address.
  2. The named object outlives the statement, so the pointer stays valid.
gcc
int v = make();
int* p = &v;   // address of a named, living object

How to prevent it

  • Materialize a temporary into a named variable before taking its address so the pointer refers to live storage.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →