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
- Store the temporary in a named variable, then take its address.
- The named object outlives the statement, so the pointer stays valid.
gcc
int v = make();
int* p = &v; // address of a named, living objectHow to prevent it
- Materialize a temporary into a named variable before taking its address so the pointer refers to live storage.
Related guides
gcc/clang "invalid conversion from 'const char*' to 'char*'" in CIFix gcc/clang "invalid conversion from 'const char*' to 'char*'" in CI - a string literal or const pointer is…
gcc/clang "cannot bind non-const lvalue reference to an rvalue" in CIFix gcc/clang "cannot bind non-const lvalue reference of type X& to an rvalue" in CI - a temporary is passed…