Is there a way to use std::cout with a LPTSTR?

Started by
17 comments, last by Bregma 18 years, 3 months ago
Quote:Original post by Zahlman
#ifdef UNICODEostream& tcout = wcout;#elseostream& tcout = cout;#endif



I think you should define tcout as wostream when working in unicode. Like this:
#ifdef UNICODEwostream &tcout = wcout;#elseostream &tcout = cout;#endif

It compiles in VS 2005 and works fine.
Advertisement
You might as well go an extra step and introduce a typedef for the stream type:
typedef std::basic_ostream<TCHAR> tostream;#ifdef UNICODEtostream & tcout = wcout;#elsetostream & tcout = cout;#endif

It's like any global variable: if you declare and define the reference in a header, then include that header in multiple compilation units and link them together, the linker is going to complain about multiple definitions. So, as usual, you need to declare it extern in your header, and then define it in one source file:

// foo.h:typedef std::ostream<TCHAR> tostream;extern tostream &tcout...// foo.cpp:#include "foo.h"...#ifdef UNICODE  tostream &tcout = std::cout;#else  tostream &tcout = std::wcout;#endif
Let's take that one step further and make that file for *just* handling the streams, and give it a decent name:

// tiostream.h#ifndef TIOSTREAM_H#define TIOSTREAM_H#include <iostream>// We won't put our references into the std namespace because that isn't nice// and we'll leave on a .h extension to indicate that we aren't a standard// library filetypedef std::ostream<TCHAR> tostream;typedef std::istream<TCHAR> tistream;extern tostream &tcoutextern tistream &tcin#endif// tiostream.h#include "tiostream.h"#ifdef UNICODE  tostream& tcout = std::wcout;  tistream& tcin = std::wcin;#else  tostream& tcout = std::cout;  tistream& tcin = std::cin;#endif


Now you can just include tiostream.h instead of iostream. :) (You *could* put the names into namespace std I guess, or perhaps preferably into your own namespace...)
Wow, thank you all.
typedef templates are not permitted in the official C++ standards. you will see compile errors with .NET C++'s compiler
Quote:Original post by Anonymous Poster
typedef templates are not permitted in the official C++ standards. you will see compile errors with .NET C++'s compiler


And you see typedef templates in this thread where?
Nobody has proposed a template typedef. The typedefs used in this thread are all typedefs of template instantiations, which are perfectly legal, i.e.:
template < typename Type >class Thing{	// stuff};// template typedef (possible syntax) - not valid standard C++// template < typename Type >// typedef Thing< Type > OtherNameForThing// OtherNameForThing< int > onft;// template typedef (proposed C++0x syntax) - not yet valid standard C++// template < typename Type >// using OtherNameForThing = Thing< Type >// OtherNameForThing< int > onft;// typedef of template instantiation - perfectly valid standard C++typedef Thing< int > IntThing;IntThing it;

Enigma

(SiCrane well and truely beat me to it because I went looking for the proposed C++0x template typedef syntax).
Original post by Zahlman
I just have to be pedantic. Consider it a personality flaw.
// tiostream.h#ifndef TIOSTREAM_H#define TIOSTREAM_H#include <istream> // for std::basic_istream#include <ostream> // for std::basic_ostream// We won't put our references into the std namespace because that isn't nice// and we'll leave on a .h extension to indicate that we aren't a standard// library filetypedef std::basic_ostream<TCHAR> tostream;typedef std::basic_istream<TCHAR> tistream;extern tostream &tcoutextern tistream &tcin#endif// tiostream.cpp#include "tiostream.h"#include <iostream> // for std::[w]cin and std::[w]cout#ifdef UNICODE  tostream& tcout = std::wcout;  tistream& tcin = std::wcin;#else  tostream& tcout = std::cout;  tistream& tcin = std::cin;#endif



Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement