Sanity Check

B

Ben Pope

Hi,

The following code compiles with Comeau Online, but not MSVC 8.0.

Comeau issues a warning that return 0 is unreachable.



#include <sstream>
#include <iostream>

class mystream : public std::stringstream {
public:
explicit mystream:):std::ios::eek:penmode xW = in | out) :
std::stringstream(xW)
{}
mystream(const mystream& rhs) : std::stringstream() {
*this << rhs.str();
}
};

int main() {
mystream s1;
s1 << "foo";

mystream s2(s1);

throw s1; // can't access private copy constructor

return 0;
}


Is this a bug?


Ben Pope
 
K

Kai-Uwe Bux

Ben said:
Hi,

The following code compiles with Comeau Online, but not MSVC 8.0.

Comeau issues a warning that return 0 is unreachable.



#include <sstream>
#include <iostream>

class mystream : public std::stringstream {
public:
explicit mystream:):std::ios::eek:penmode xW = in | out) :
std::stringstream(xW)
{}
mystream(const mystream& rhs) : std::stringstream() {
*this << rhs.str();
}
};

int main() {
mystream s1;
s1 << "foo";

mystream s2(s1);

throw s1; // can't access private copy constructor

return 0;
}

a) This code compiles just fine in g++-4.0.2

b) I do not see any reason, why it shouldn't compile.

Is this a bug?

Where?



Best

Kai-Uwe Bux
 
B

Ben Pope

Kai-Uwe Bux said:
a) This code compiles just fine in g++-4.0.2

b) I do not see any reason, why it shouldn't compile.



Where?

Exactly. It can find the copy constructor when I do:
mystream s2(s1);

I get the following error (which I should have posted earlier) Line 20
is the throw statement, if I comment it, it compiles fine:



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 ==========


Ben Pope
 
R

Rolf Magnus

Kai-Uwe Bux said:
a) This code compiles just fine in g++-4.0.2

Well, it does warn:

In copy constructor 'mystream::mystream(const mystream&)':
warning: base class 'struct std::basic_ios<char, std::char_traits<char> >'
should be explicitly initialized in the copy constructor

This is probably due to virtual inheritance being involved.
 
B

Ben Pope

Rolf said:
Well, it does warn:

In copy constructor 'mystream::mystream(const mystream&)':
warning: base class 'struct std::basic_ios<char, std::char_traits<char> >'
should be explicitly initialized in the copy constructor

This is probably due to virtual inheritance being involved.

I can't seem to make it work, how should I go about initialising that base?

Ben Pope
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top