After putting some code to work, I’m trying to optimize it. One thing that I’m trying to avoid is the creation of temporaries. Given a class
class foo{
private:
int a;
public:
foo(int sa):a(sa);
~foo(){}
inline int multiply(int b) {return a*b;}//temporary?
};
The intel compiler is giving me the temporary created/reference temporary used here. Is any temporary variable being created?
EDIT: edited the return type. Also, I think that no temporary variable is being created in multiply, but the intel compiler gives me an error about that.
EDIT 2: After the requests below, here is a FULL code that the intel compiler v. 12 gives the error:
#include <iostream>
#include <complex>
using namespace std;
const double pi = 3.1415;
const complex <double> I = (0.0,I);
const complex <double> oneover2piI = (1.0 / (2.0 * pi * I));
class foo{
private:
complex <double> a;
public:
foo(complex <double> sa):a(sa){}
~foo(){}
inline complex <double> multiply(complex<double> b)
{return a*sqrt(b);}
};
The compilation line is
icc -g -O2 -w2 -debug parallel -Wcheck -Weffc++ -mp -fp-stack-check -wd981 -wd2015 -wd1418 test.cpp -o test
And the resulting warning is
test.cpp(7): remark #383: value copied to temporary, reference to temporary used
const complex <double> oneover2piI = (1.0 / (2.0 * pi * I));
^
The question was to understand what the compiler is balking at, and not to be hanous.
Thanks!
I think the compiler issues a warning because it translates:
to invoke a default copy constructor:
which takes a reference of the temporary object as the argument, and makes a shallow copy (that results in data sharing if you use pointers) of the object. Basically this data sharing + temporary lifespan is the core of the problem. Take a look at the “academic” class below.
the cygwin output:
The
deleteoperation has been executed twice on the same pointer, because it was shared between the temporary object and a.