From: Ben Pope on 18 Feb 2006 13:50 David Wilkinson wrote: > Bo Persson wrote: > >> "Ben Pope" <benpope81_remove_(a)gmail.com> skrev i meddelandet >> news:dt70tj$nvo$1(a)gemini.csx.cam.ac.uk... >> >>> Carl Daniel [VC++ MVP] wrote: >>> >>>> Ben Pope wrote: >>>> >>> I can't see where the copy constructor of stringstream is used. Do I >>> not construct the stringstream base seperately? >> >> >> It's hidden in the throw statement. >> >> Before leaving the function containing the throw statement, the throw >> machanism will have to make a copy of the thrown object. Otherwise it >> will be lost. >> >> >> Bo Persson >> > > Bo; > > I think he knows that. But he has written his own mystream copy > constructor, which does not use the stringstream copy constructor. Exactly. > If it compiles with Comeau, it can't be wrong! Yeah, that's the general rule. Apparently g++ 4.0.2 warns about the ios_base should be explicitly initialised. I don't see why though. VC7.1 compiles it (with a cast to mystream at the throw site - different problem). Ben Pope -- I'm not just a number. To many, I'm known as a string...
From: Ben Pope on 19 Feb 2006 13:35 Ben Pope wrote: > > #include <sstream> > #include <iostream> > > class mystream : public std::stringstream { > public: > explicit mystream(::std::ios::openmode xW = in | out) : > std::stringstream(xW) > {} > mystream(const mystream& rhs) : std::stringstream() { > *(static_cast<std::stringstream*>(this)) << rhs.str(); > } > }; > > int main() { > mystream s1; > s1 << "foo"; > > mystream s2(s1); > > throw s1; // Line 20 > > return 0; > } Line 20 complains about copy-constructor > Is this a bug? If so, is there a workaround? Anybody? I'm trying to compile XiMoL (a C++ XML binding library), and this is basically the problem I have. Cheers. Ben Pope -- I'm not just a number. To many, I'm known as a string...
From: Tom Widmer [VC++ MVP] on 20 Feb 2006 08:47 Ben Pope wrote: >> I think he knows that. But he has written his own mystream copy >> constructor, which does not use the stringstream copy constructor. > > > Exactly. > >> If it compiles with Comeau, it can't be wrong! > > > Yeah, that's the general rule. > > Apparently g++ 4.0.2 warns about the ios_base should be explicitly > initialised. I don't see why though. basic_ios is a virtual base class, so it is initialized by the most derived class. You could be explicit in your virtual base initialization by doing this: mystream(const mystream& rhs) : std::basic_ios<char, std::char_traits<char> >(), //default initialize std::stringstream() { *(static_cast<std::stringstream*>(this)) << rhs.str(); } to quell the warning. It is just a warning though. I agree it looks like a bug, assuming your test program is exactly as posted (I don't have 2005 installed at the moment). Tom
From: Ben Pope on 20 Feb 2006 09:20 Tom Widmer [VC++ MVP] wrote: > Ben Pope wrote: > > > basic_ios is a virtual base class, so it is initialized by the most > derived class. You could be explicit in your virtual base initialization > by doing this: > mystream(const mystream& rhs) : > std::basic_ios<char, std::char_traits<char> >(), //default initialize > std::stringstream() { > *(static_cast<std::stringstream*>(this)) << rhs.str(); > } > > to quell the warning. It is just a warning though. Yeah, well I don't use g++, so that's pretty academic. > I agree it looks like a bug, assuming your test program is exactly as > posted (I don't have 2005 installed at the moment). Exactly as posted. I tried with the initialisation of basic_ios as above, but it doesn't solve the problem in VC8. The throw statement seems to force creation of the copy constructor of the base class (stringstream), which obviously fails because the copy constructor of the base class of stringstream is also private. The same problem appears in my own test, if I create a base with copy constructor declared private, and not defined, throwing a derived class (with a copy constructor that does not use the base copy constructor) requires creation of the copy constructor of the base. This results in a link error. Defining the base copy constructor fixes it, and the copy constructor is not called: class base { public: base() {} private: // noncopyable base(const base&);// {} //(don't) define it base& operator=(const base&); }; class derived : public base { public: derived() {} derived(const derived&) : base() {} private: // not assignable derived& operator=(const derived&); }; int main() { derived d1; // OK derived d2(d1); // OK // Forces creation of base copy-constructor which fails. throw d1; } Unfortunately I don't have the same flexibility (modifying the base) when inheriting from the STL. I may have to use containment of the stringstream and wrapping the member functions I need, instead of public inheritance. A PITA but I see no other way. Ben Pope -- I'm not just a number. To many, I'm known as a string...
First
|
Prev
|
Pages: 1 2 Prev: VC++ 7.1 Stack Overflow Next: Why can the program, osk.exe, be run before logon? |