Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking address of temporary - workaround needed

I am facing a GCC warning that I want to fix. Basically I am passing to a method a pointer to a local variable, which in my case is perfectly OK. I understand why the compiler tells me that this is a potential problem, but in my case this is OK.

How can I workaround it, on a local space? Passing -fpermissive when compiling will make me fail to find future problems. I want to fix this specific problem, or workaround it.

Code is available here:

#include <cstdio>

class Integer{
public:
    Integer(int i ){ v = i; };
    int value(){ return v; };
private:
    int v;
};

int foo(Integer *i);

int main()
{
    foo( &Integer(12) );
}

int foo(Integer *i)
{
    std::printf("Integer = %d\n", i->value());
}

And compilation gives me:

$ g++ test-reference.cpp -O test-reference
test-reference.cpp: In function ‘int main()’:
test-reference.cpp:15:18: error: taking address of temporary [-fpermissive]

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu3) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

EDIT:

Using const (as in making foo take a const pointer, and marking value() as const) gives the same error.

like image 716
elcuco Avatar asked Apr 01 '12 08:04

elcuco


3 Answers

Integer i(12);
foo(&i);

That gets rid of the "taking the address of a temporary" problem, which is what you have. You're not passing the address of a local variable (which the above does, and is indeed ok in this case), you're grabbing the address of a temporary.

Obviously, if foo tries to hold on to that pointer one way or another, you'll have issues down the line.

like image 97
Mat Avatar answered Nov 09 '22 16:11

Mat


template<typename T> const T* rvalue_address(const T& in) {
    return &in;
}

In my opinion, it should be just as legal to take a const T* as a const T&, but this trivial function will handily perform the conversion.

like image 22
Puppy Avatar answered Nov 09 '22 18:11

Puppy


GCC is wrong in this case. Your integer is an rvalue and taking the address of an rvalue is illegal.

§5.3.1 Unary operators, Section 3

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id.

Clang gives an error in this case:

error: taking the address of a temporary object of type 'Integer' [-Waddress-of-temporary]
    foo( &Integer(12) );
         ^~~~~~~~~~~~
like image 32
Nikola Smiljanić Avatar answered Nov 09 '22 16:11

Nikola Smiljanić