Re: error: value-initialization of reference
"David Hammarwall" <david.hammarwall@gmail.com>
I've got a problem using a library where I get the compiler error
"value-initialization of reference" (using gcc4.3.2). I've isolated
the problem to the following dummy example:
int DEFAULT_OBJ = 0;
class Class_With_Ref
{
public:
Class_With_Ref() : ref(DEFAULT_OBJ) {}
Class_With_Ref& operator=(const Class_With_Ref &v) {return *this;}
private:
int &ref;
};
What is the point to have a state variable if it has the same value in every
instance?
Why not just make a static int
class Class_
{
public:
Class_() {} // not really needed if nothing else
// copy ctor/op= good as created
private:
static int DEFAULT_OBJ;
};
?
struct Wrapper
{
// Wrapper(){} //Everything will work if this line is un-commented
Class_With_Ref test;
};
int main(int argc, char **argv) {
Wrapper t = Wrapper();
return 1;
}
Everything will work if I explicitly declare (the empty) default
constructor in the Wrapper struct,
That is interesting, I would guess the code shall do the same with or
without that ctor.
Trying in Cameau C++: yeah, it compiles both commented and uncommented
version fine. So you have a compiler bug.
but this is a pain considering that
there are a plethora of structs that contains a Class_With_Ref
objects. Moreover, I did not have this issue with gcc 3. Also, the
assignment, Wrapper t = Wrapper(),
nitpick: it is not assignment but initialization.
is implemented in the library so I
cannot change it to e.g.,
Wrapper t,t2;
t=t2;
What you absolutely shouldn't.
which works perfectly in the above example.
Does anybody have any insight here?
IMO, change compiler (version). If your code is full of this stuff, and it
hits a bug, chances are high the same version could mess up code generation
too in a less noisy way. Regardless of getting rid of the phony reference
and faked assignment (that btw is not used in the example code only in the
'workaround').
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]