473,698 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::iostream/std::cout - Do not compile with vc-8_0

Hi all,

Something I don't get.
The code is:

// snippet on
#include <list>
#include <iostream>

int main()
{
std::list<std:: ostreamlist;
list.push_back( std::cout);
return 0
}
// snippet off

Compiles with msvc (visual 6) but not with vc-8_0 (visual 2005).

vc-8_0 output (C:\>cl /EHsc test.cpp) :

// output on
test.cpp
G:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\os tream(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::ch ar_traits<char>
]
G:\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::ch ar_traits<char>
]
This diagnostic occurred in the compiler generated function
'std::basic_ost ream<_Elem,_Tra its>::basic_ost ream(const
std::basic_ostr eam<_Elem,_Trai ts&)'
with
[
_Elem=char,
_Traits=std::ch ar_traits<char>
]
// output off

Why ? std::cout is supposed to be a std::ostream right ? I tried a
static_cast<std ::ostream &>, but no luck...

Any idea?

JD

Mar 28 '07 #1
8 3464
<je************ *****@gmail.com wrote in message
news:11******** *************@p 15g2000hsd.goog legroups.com...
Hi all,

Something I don't get.
The code is:

// snippet on
#include <list>
#include <iostream>

int main()
{
std::list<std:: ostreamlist;
list.push_back( std::cout);
return 0
}
// snippet off

Compiles with msvc (visual 6) but not with vc-8_0 (visual 2005).

vc-8_0 output (C:\>cl /EHsc test.cpp) :

// output on
test.cpp
G:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\os tream(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::ch ar_traits<char>
]
G:\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::ch ar_traits<char>
]
This diagnostic occurred in the compiler generated function
'std::basic_ost ream<_Elem,_Tra its>::basic_ost ream(const
std::basic_ostr eam<_Elem,_Trai ts&)'
with
[
_Elem=char,
_Traits=std::ch ar_traits<char>
]
// output off
Standard library containers (such as std::list) impose requirements
upon the types of object they can contain. These requirements are
that these objects must be copyable and assignable. Stream types
do not meet this requirement. If you must group streams into a
'collection', you'll need to store pointers to them instead of
the stream objects themselves. BTW what specifically is your
intended purpose of using a container of stream objects?
>
Why ? std::cout is supposed to be a std::ostream right ? I tried a
static_cast<std ::ostream &>, but no luck...
Don't guess. Using a cast to 'fix' a compiler warning or error is
almost always the wrong thing to do.

-Mike
Mar 28 '07 #2
>
Standard library containers (such as std::list) impose requirements
upon the types of object they can contain. These requirements are
that these objects must be copyable and assignable. Stream types
do not meet this requirement. If you must group streams into a
'collection', you'll need to store pointers to them instead of
Ok, but why msvc would compile it then ? And why this compiles with
vc-8_0:
// snippet on
#include <list>
#include <iostream>

int main()
{
std::list<std:: ostreamlist;
return 0;
}
// snippet off

I have created a list of ostream... It should not compile has ostream
does not satisfies the requirements you enounce.
the stream objects themselves. BTW what specifically is your
intended purpose of using a container of stream objects?

I'm trying to write a log library. This list of streams is just the
list of output for my logger object. Make use of pointers here will
just make the usage of the library harder. User will have to use new,
and therefore delete...
>
Why ? std::cout is supposed to be a std::ostream right ? I tried a
static_cast<std ::ostream &>, but no luck...

Don't guess. Using a cast to 'fix' a compiler warning or error is
almost always the wrong thing to do.
I know, I saw this on a previous post on the same pb, so I wanted to
step aside this potential answer.

JD

Mar 28 '07 #3
je************* ****@gmail.com wrote:
>Standard library containers (such as std::list) impose requirements
upon the types of object they can contain. These requirements are
that these objects must be copyable and assignable. Stream types
do not meet this requirement. If you must group streams into a
'collection' , you'll need to store pointers to them instead of

Ok, but why msvc would compile it then ? And why this compiles with
vc-8_0:
// snippet on
#include <list>
#include <iostream>

int main()
{
std::list<std:: ostreamlist;
return 0;
}
// snippet off

I have created a list of ostream... It should not compile has ostream
does not satisfies the requirements you enounce.
You've not done anything that would actually invoke the code requiring
the functionality 'ostream' lacks. Try pushing anything in.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 28 '07 #4
je************* ****@gmail.com wrote:
>Standard library containers (such as std::list) impose requirements
upon the types of object they can contain. These requirements are
that these objects must be copyable and assignable. Stream types
do not meet this requirement. If you must group streams into a
'collection' , you'll need to store pointers to them instead of

Ok, but why msvc would compile it then ? And why this compiles with
vc-8_0:
// snippet on
#include <list>
#include <iostream>

int main()
{
std::list<std:: ostreamlist;
return 0;
}
// snippet off
msvc doesn't actually instantiate any template source code that would
facilitate the missing copy semantic in std::ostream. It only
instantiates upon demand.
Mar 28 '07 #5
On Mar 29, 6:51 am, jean.daniel.mic h...@gmail.com wrote:
Standard library containers (such as std::list) impose requirements
upon the types of object they can contain. These requirements are
that these objects must be copyable and assignable. Stream types
do not meet this requirement. If you must group streams into a
'collection', you'll need to store pointers to them instead of

Ok, but why msvc would compile it then ? And why this compiles with
vc-8_0:
Most compilers still compile some programs even though they don't
comply with the C++ standard.
Mar 28 '07 #6
>
Standard library containers (such as std::list) impose requirements
upon the types of object they can contain. These requirements are
that these objects must be copyable and assignable. Stream types
do not meet this requirement. If you must group streams into a
'collection', you'll need to store pointers to them instead of
the stream objects themselves. BTW what specifically is your
intended purpose of using a container of stream objects?
Ok, so to avoid using pointers, I'm trying the shared_ptr, but I still
have difficulties with ostream.
In this code, I add to a list a reference to an ostream. When I try to
access it, I have a crash.

// Snippet on
#include <list>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>

int main()
{
std::list<boost ::shared_ptr<st d::ostream l;

boost::shared_p tr<std::ostream p(&std::cout);
*p << "on the shared pointer" << std::endl;
l.push_back(p);

std::list<boost ::shared_ptr<st d::ostream::ite rator it =
l.begin();
std::cout << "before loop" << std::endl;
while (it++ != l.end())
{
boost::shared_p tr<std::ostream p2 = *it;
*p2 << "test" << std::endl;
}

return 0;
}
// snippet off

I have somthing like:
// output on
on the shared pointer
before loop
segmentation fault
// output off

dereferencing p2 should give me my ostream. It works on p... Any idea?

JD

Mar 29 '07 #7
On Mar 29, 10:44 am, jean.daniel.mic h...@gmail.com wrote:
Ok, so to avoid using pointers, I'm trying the shared_ptr, but I still
have difficulties with ostream.
In this code, I add to a list a reference to an ostream. When I try to
access it, I have a crash.

// Snippet on
#include <list>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>

int main()
{
std::list<boost ::shared_ptr<st d::ostream l;

boost::shared_p tr<std::ostream p(&std::cout);
*p << "on the shared pointer" << std::endl;
l.push_back(p);

std::list<boost ::shared_ptr<st d::ostream::ite rator it =
l.begin();
std::cout << "before loop" << std::endl;
while (it++ != l.end())
{
boost::shared_p tr<std::ostream p2 = *it;
*p2 << "test" << std::endl;
}

return 0;}

// snippet off

I have somthing like:
// output on
on the shared pointer
before loop
segmentation fault
// output off

dereferencing p2 should give me my ostream. It works on p... Any idea?

JD
First off, a boost::shared_p ointer will delete its pointee by default.
You can overcome that by passing in a null deleter to the shared
pointer constructor (details in the boost::shared_p tr docs). With what
you have, the shared pointer will call delete on &cout when the last
shared pointer is destructed (at the end of the program). This will
almost certainly cause a crash of some sort.

I'm not sure why you're getting a segfault before "test" is output,
since std::endl is supposed to flush buffers (I believe). If I'm
wrong, your program could be crashing at program termination due to
what I said above, but before all the buffers are flushed (certainly,
if you had used <<"\n" instead of <<std::endl, thats what I would have
guessed).

I'm not sure that what you are doing is necessarily the best way to
achieve what you want, but as I have no ther suggestions, try using a
null deleter, and report back.

-matt

Mar 29 '07 #8
First off, a boost::shared_p ointer will delete its pointee by default.
You can overcome that by passing in a null deleter to the shared
pointer constructor (details in the boost::shared_p tr docs). With what
you have, the shared pointer will call delete on &cout when the last
shared pointer is destructed (at the end of the program). This will
almost certainly cause a crash of some sort.
Yes obviously, I forget that but that is obvious.
Here is the code, that produce the same result.

// snippet on
#include <list>
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>

struct null_deleter { void operator()(void const *) const {} };

int main()
{
std::list<boost ::shared_ptr<st d::ostream l;

boost::shared_p tr<std::ostream p(&std::cout, null_deleter()) ;
*p << "on the shared pointer" << std::endl;
l.push_back(p);

std::list<boost ::shared_ptr<st d::ostream::ite rator it =
l.begin();
std::cout << "before loop" << std::endl;
while (it++ != l.end())
{
boost::shared_p tr<std::ostream p2 = *it;
*p2 << "test" << std::endl;
}

return 0;
}
// snippet off
I'm not sure why you're getting a segfault before "test" is output,
since std::endl is supposed to flush buffers (I believe). If I'm
wrong, your program could be crashing at program termination due to
what I said above, but before all the buffers are flushed (certainly,
if you had used <<"\n" instead of <<std::endl, thats what I would have
guessed).
Actually it really crashes on the streaming. I debugged it with gdb.
It crashes on:
std::ostream::s entry::sentry () from /usr/lib/libstdc++.so.6
I'm not sure that what you are doing is necessarily the best way to
achieve what you want, but as I have no ther suggestions, try using a
null deleter, and report back.
What I am trying to do is to have a pool of streams where to write on.
The user add streams to the list, pass this to my library, and then I
shall manage deallocation. I don't want the user to mess around with
shared_ptr and I dont want him to allocate pointer either (RIIA, RAAI
or RIAA, I don't remember the term :) ...)

Thanks for your help.

JD

Mar 30 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3298
by: Pmb | last post by:
I've noticed a lot of people preferring std::cout over cout. May I ask why you prefer one over the other? thanks Pmb
12
3558
by: Minti | last post by:
Is std::cout slower than printf When we call printf e.g. in printf(20 format conversion specifications, 20 arguments); Is it faster than the std::cout << { 20 redirections to the output stream }
3
1635
by: Jason Heyes | last post by:
I have a function that reads tags. The function prints any failures it encounters to std::cout. std::istream &read_tag(std::istream &is, std::string &tag) { std::string s; if (!(is >> s)) { std::cout << "Failed to read string of tag" << std::endl; return is;
12
6093
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
3
6666
by: Jim Langston | last post by:
I have a computer game that has no console. If I want to output something I push a string to the back of a std::vector<std::stringand that gets output and maintained. All is well and good, but what I would like to do is to be able to use ostream's power for this output. I could create my own ostream and check it every cycle or something, but then I would have to pass this ostream or make it global. What I would like to do is have...
7
4691
by: imutate | last post by:
How do I implement << ala std::cout for vector template ? I already have the following: #include <vector> template < typename T > class Vec : public std::vector< T { public: Vec() { } Vec( int s ) : std::vector<T>(s) { }
19
33405
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can show the number:
2
2097
by: gwaldron | last post by:
Hi, I have a mixed-mode C++/CLI assembly. "std::cout" generates no output when I compile in Release mode and run from inside VS2005. In Debug mode, it works fine. Can anyone offer any pointers? It would be greatly appreciated. Thanks. Glenn
11
6734
by: Adrian | last post by:
Is it possible to save a copy of cout the same way you can save a copy of stdout in C (I know C version is portable, but unix portable is good enough for me). I have to use a library which closes stdout on init because its used to mostly in background processes but I need it for an interactive process. Library cant be changed unfortunately. I tried the following but the C++ version doesn't work, I guess because it only shares the...
58
4819
by: Mark Casternoff | last post by:
I'm getting back into C++ after a long hiatus (they didn't have namespaces back then). I know this question is completely subjective, but I'd be interested in hearing which is the "better" style and what the pros and cons are (I'm using cout as example, but it really applies to any similar construct): 1) using std::cout;
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.