Narrow Vs. wide streams

D

Dave

The program below is conforming according to VC++ 7.1 and Comeau online. I
would have expected a type mismatch. Am I seeing a compiler problem, or is
the observed behavior correct?

Thanks!


#include <iostream>

using namespace std;

int main()
{
// Displays a hex number under VC++ 7.1
cout << L"Hello world!" << endl;

// Displays "Hello world!" under VC++ 7.1
wcout << "Hello world!" << endl;
}
 
J

Jonathan Turkanis

Efrat said:
Your operator<< call calls a method of the form

Imp &operator<<(const void *p_val)

in both cases.

In the first case only: narrow C-style strings can be written to any
basic_ostream: chars are widened using a ctype facet.
The subsequent "translation" from void * to char * or
w_char * is handled by the stream, and is independent from
what p_val originally pointed to (as far as I understand).

Jonathan
 
E

Efrat Regev

Dave said:
The program below is conforming according to VC++ 7.1 and Comeau online. I
would have expected a type mismatch. Am I seeing a compiler problem, or is
the observed behavior correct?

Thanks!


#include <iostream>

using namespace std;

int main()
{
// Displays a hex number under VC++ 7.1
cout << L"Hello world!" << endl;

// Displays "Hello world!" under VC++ 7.1
wcout << "Hello world!" << endl;
}

Your operator<< call calls a method of the form

Imp &operator<<(const void *p_val)

in both cases. The subsequent "translation" from void * to char * or w_char
* is handled by the stream, and is independent from
what p_val originally pointed to (as far as I understand).
 
E

Efrat Regev

Jonathan Turkanis said:
In the first case only: narrow C-style strings can be written to any
basic_ostream: chars are widened using a ctype facet.


Jonathan

Yes, you're right. Sorry for my mistaken reply.
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top