Re: error: value-initialization of reference

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,


On Thu, Sep 10, 2009 at 05:33:11PM +0200, David Hammarwall wrote:
> 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?

I fear its a bug - but I'm not sure about such specalized details of the
C++-Standard.

To be more specific: I (gcc 4.3.2) get the error from the call "Wrapper()".  

Wrapper t;

works without problem - and calls the default constructor of
Class_With_Ref, thus initializing the reference.
On the other hand:

Wrapper();

creates the error. According to my understanding of the C++-standard,
both these lines ("Wrapper t;" and "Wrapper();) should call th
implicitely defined Constructor for "Wrapper". And this should be
equivalent to "Wrapper(){}" - that is, an empty function without member
initialization list (§12.1.7).
As Class_With_Ref contains a reference, it is NOT a POD-class-type,
such that it should be default-initialized and its default constructor
should be called automatically - even, when it is not initialized in the
member initialization list of the constructor of Wrapper.

So everything should probably work correctly - did you try already a
more recent version of gcc?

Axel



> 
> BR,
> David H
> 
> 
> 
> 
> 2009/9/10 John S. Fine <johnsfine@xxxxxxxxxxx>:
> > 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
> >>
> >>
> >
> >

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux