From: Ben Pope on
Hi,

I don't seem to be able to successfully compile with line 20
uncommented. This compiles with Comeau online and GCC 4.0.2 (apparently).

A slight variation of this works in VC 7.1 (with a cast).

Code follows:


#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;
}


I get the following error:

------ Build started: Project: test, Configuration: Debug Win32 ------
Compiling...
test.cpp
c:\development\cpp\smalltest\test\test\test.cpp(20) : warning C4673:
throwing 'mystream' the following types will not be considered at the
catch site
c:\program files\microsoft visual studio 8\vc\include\sstream(513) :
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access
private member declared in class 'std::basic_ios<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ios(151)
: see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
This diagnostic occurred in the compiler generated function
'std::basic_stringstream<_Elem,_Traits,_Alloc>::basic_stringstream(const
std::basic_stringstream<_Elem,_Traits,_Alloc> &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Alloc=std::allocator<char>
]
c:\program files\microsoft visual studio 8\vc\include\istream(916) :
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access
private member declared in class 'std::basic_ios<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ios(151)
: see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
This diagnostic occurred in the compiler generated function
'std::basic_iostream<_Elem,_Traits>::basic_iostream(const
std::basic_iostream<_Elem,_Traits> &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\istream(842) :
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access
private member declared in class 'std::basic_ios<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ios(151)
: see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
This diagnostic occurred in the compiler generated function
'std::basic_istream<_Elem,_Traits>::basic_istream(const
std::basic_istream<_Elem,_Traits> &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(581) :
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access
private member declared in class 'std::basic_ios<_Elem,_Traits>'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ios(151)
: see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
This diagnostic occurred in the compiler generated function
'std::basic_ostream<_Elem,_Traits>::basic_ostream(const
std::basic_ostream<_Elem,_Traits> &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
Build log was saved at
"file://c:\Development\Cpp\smalltest\test\test\Debug\BuildLog.htm"
test - 4 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Is this a bug? If so, is there a workaround?

Thanks for your time,

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
From: Carl Daniel [VC++ MVP] on
Ben Pope wrote:
> Hi,
>
> I don't seem to be able to successfully compile with line 20
> uncommented. This compiles with Comeau online and GCC 4.0.2
> (apparently).
> A slight variation of this works in VC 7.1 (with a cast).

Unless I'm missing something obvious, according to 27.7.5 of the C++
standard, there's no requirement that std::stringstream be copy
constructible. If that's the case, then all of the standard libraries you
cited, including VC8, are conforming, but code that relies on the ability to
copy-construct a stringstream is not.

-cd


From: Ben Pope on
Carl Daniel [VC++ MVP] wrote:
> Ben Pope wrote:
>> Hi,
>>
>> I don't seem to be able to successfully compile with line 20
>> uncommented. This compiles with Comeau online and GCC 4.0.2
>> (apparently).
>> A slight variation of this works in VC 7.1 (with a cast).
>
> Unless I'm missing something obvious, according to 27.7.5 of the C++
> standard, there's no requirement that std::stringstream be copy
> constructible. If that's the case, then all of the standard libraries you
> cited, including VC8, are conforming, but code that relies on the ability to
> copy-construct a stringstream is not.

I can't see where the copy constructor of stringstream is used. Do I
not construct the stringstream base seperately?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
From: Bo Persson on

"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:
>>> Hi,
>>>
>>> I don't seem to be able to successfully compile with line 20
>>> uncommented. This compiles with Comeau online and GCC 4.0.2
>>> (apparently).
>>> A slight variation of this works in VC 7.1 (with a cast).
>>
>> Unless I'm missing something obvious, according to 27.7.5 of the
>> C++ standard, there's no requirement that std::stringstream be copy
>> constructible. If that's the case, then all of the standard
>> libraries you cited, including VC8, are conforming, but code that
>> relies on the ability to copy-construct a stringstream is not.
>
> 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


From: David Wilkinson on
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:
>>>
>>>>Hi,
>>>>
>>>>I don't seem to be able to successfully compile with line 20
>>>>uncommented. This compiles with Comeau online and GCC 4.0.2
>>>>(apparently).
>>>>A slight variation of this works in VC 7.1 (with a cast).
>>>
>>>Unless I'm missing something obvious, according to 27.7.5 of the
>>>C++ standard, there's no requirement that std::stringstream be copy
>>>constructible. If that's the case, then all of the standard
>>>libraries you cited, including VC8, are conforming, but code that
>>>relies on the ability to copy-construct a stringstream is not.
>>
>>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.

If it compiles with Comeau, it can't be wrong!

David Wilkinson