P
Pavel
In the code below, the increment on a function call expression of type "int"
does not compile but the one on a function call expression of class Int compiles
fine. My understanding is (please correct me if I am wrong) that it directly
follows from 3.10 and 5.3-2 of the current Standard and (also please correct me
if I am wrong here) that this is no different in the upcoming C++0x Standard.
What is the rationale for this lvalue requirement if there is one?
My suspicion is that it may be related to an optimization opportunity for a C++
compiler but I could not come up with a satisfactory guess on what it could be.
To rephrase my question, am I writing a potentially sub-optimal code if I always
use a thin class wrapper around a fundamental integral or pointer type as
opposed to using it outright? (I use a wrapper, in particular, to be able to
call ++ on the function's return value; this is an advantage in my case -- but
is there a downside other than having to write the wrapper?)
TIA,
-Pavel
-----------------
int f1() { return 5; }
class Int {
public:
explicit Int(int i) : i_(i) {}
Int & operator++() { ++i_; return *this; }
private:
int i_;
};
Int f2() { return Int(5); }
int main(int, char *[]) {
//int i1 = ++f1(); (void)i1; // this does not compile
Int i2 = ++f2(); (void)i2; // this compiles fine
return 0;
}
does not compile but the one on a function call expression of class Int compiles
fine. My understanding is (please correct me if I am wrong) that it directly
follows from 3.10 and 5.3-2 of the current Standard and (also please correct me
if I am wrong here) that this is no different in the upcoming C++0x Standard.
What is the rationale for this lvalue requirement if there is one?
My suspicion is that it may be related to an optimization opportunity for a C++
compiler but I could not come up with a satisfactory guess on what it could be.
To rephrase my question, am I writing a potentially sub-optimal code if I always
use a thin class wrapper around a fundamental integral or pointer type as
opposed to using it outright? (I use a wrapper, in particular, to be able to
call ++ on the function's return value; this is an advantage in my case -- but
is there a downside other than having to write the wrapper?)
TIA,
-Pavel
-----------------
int f1() { return 5; }
class Int {
public:
explicit Int(int i) : i_(i) {}
Int & operator++() { ++i_; return *this; }
private:
int i_;
};
Int f2() { return Int(5); }
int main(int, char *[]) {
//int i1 = ++f1(); (void)i1; // this does not compile
Int i2 = ++f2(); (void)i2; // this compiles fine
return 0;
}