error: value-initialization of reference

David Hammarwall david.hammarwall@gmail.com
Thu Sep 10 15:33:00 GMT 2009


Hi,

Actually I get the same error message if I implement it as:

Wrapper t;
t = Wrapper();

Also, it does not matter if the copy constructor is defined or not in
Class_With_Ref, I still get the same error message. I also get the
error message if I use
const int &ref as a member to Class_With_Ref.

What I want the code to do is: The code should do the same as if the
default constructor in Wrapper was defined. (Note that everything
works perfectly if the constructor is explicitly defined)

The question remans: Is this a bug or feature of gcc4.3.2?

BR,
David H




2009/9/10 John S. Fine <johnsfine@verizon.net>:
> What do you expect/want the copy constructor of Class_With_Ref to do?
>
> I'm not quite sure of the exact C++ standard meaning of
>
> Wrapper t = Wrapper();
>
> (what constructors and/or operator=() does it, call under what conditions.)
> so this issue may not be involved.  But in any case it seems likely that
> some point in your code is invoking a copy constructor on Class_With_Ref and
> if you don't define that copy constructor, the compiler will and the one the
> compiler will define will be wrong.
>
> The source of the copy constructor would be a const Class_With_Ref& so I
> think there is a problem initializing the int&ref in the object being
> constructed from the int&ref in the const source.
>
> David Hammarwall wrote:
>>
>> Hi,
>>
>> 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;
>> };
>>
>> 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, 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(), is implemented in the library so I
>> cannot change it to e.g.,
>>
>> Wrapper t,t2;
>> t=t2;
>>
>> which works perfectly in the above example.
>>
>> Does anybody have any insight here?
>>
>> BR,
>> David
>>
>>
>
>



More information about the Gcc-help mailing list