Click here to Skip to main content
11,496,146 members (167 online)
Click here to Skip to main content

A TCHAR style header file for STL strings and streams

, 11 Feb 2006 CPOL 70.8K 387 33
Presents a header file that allows a single source file that uses STL to be built in either ANSI or Unicode, without any changes or lots of #ifdefs.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

Introduction

I had asked on the Visual C++ forum if there was a standard header file that would provide a way of declaring std::strings so that a single code source could be compiled in either ANSI or UNICODE builds without any changes, much in the same way tchar.h is used. The response I got back is that there is no standard way of doing this so I decided to write my own header file to accomplish this task.

The TSTL.H header file

The header file is a simple one that uses a series of typedefs to define new synonyms for some of the commonly used STL classes that use chars and wchar_ts. The idea being that you can now use new names wherever you would have previously had to use either one or the other of the standard names. For example, a piece of code may have been written:

#ifdef _UNICODE
   std::wostringstream stream;
   stream.setf(std::wios::fixed);
#else
   std::ostringstream stream;
   stream.setf(std::ios::fixed);
#endif

and can now be written:

std::tostringstream stream;
stream.setf(std::tios::fixed);

and it will compile in either ANSI or Unicode builds. The file is probably not complete as there may be other STL classes that I do not know about or did not think of that should be included in this file. If there are, let me know which ones and I can include them and update the file.

To use the file, simply include it in your precompiled header file (stdafx.h) or include it in any file that uses any of the STL string or stream classes. The file is listed here and it can be downloaded from the download link at the top of this article.

// tstl.h - header file for TCHAR equivalents of STL
//          string and stream classes
//
// Copyright (c) 2006 PJ Arends
//
// This file is provided "AS-IS". Use and/or abuse it
// in any way you feel fit.
// 
 
#pragma once

#include <string>
 
namespace std
{
#if defined UNICODE || defined _UNICODE
 
    typedef wstring         tstring;
 
    typedef wstringbuf      tstringbuf;
    typedef wstringstream   tstringstream;
    typedef wostringstream  tostringstream;
    typedef wistringstream  tistringstream;
 
    typedef wstreambuf      tstreambuf;
 
    typedef wistream        tistream;
    typedef wiostream       tiostream;
 
    typedef wostream        tostream;
 
    typedef wfilebuf        tfilebuf;
    typedef wfstream        tfstream;
    typedef wifstream       tifstream;
    typedef wofstream       tofstream;
 
    typedef wios            tios;
 
#   define tcerr            wcerr
#   define tcin             wcin
#   define tclog            wclog
#   define tcout            wcout
 
#else // defined UNICODE || defined _UNICODE
 
    typedef string          tstring;
 
    typedef stringbuf       tstringbuf;
    typedef stringstream    tstringstream;
    typedef ostringstream   tostringstream;
    typedef istringstream   tistringstream;
 
    typedef streambuf       tstreambuf;
 
    typedef istream         tistream;
    typedef iostream        tiostream;
 
    typedef ostream         tostream;
 
    typedef filebuf         tfilebuf;
    typedef fstream         tfstream;
    typedef ifstream        tifstream;
    typedef ofstream        tofstream;
 
    typedef ios             tios;
 
#   define tcerr            cerr
#   define tcin             cin
#   define tclog            clog
#   define tcout            cout
 
#endif // defined UNICODE || defined _UNICODE
} // namespace std

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

PJ Arends
President
Canada Canada
No Biography provided

Comments and Discussions

 
GeneralNot so sure about adding to the std namespace... how about this solution. Pin
Ludvik Jerabek26-Sep-09 11:25
memberLudvik Jerabek26-Sep-09 11:25 
GeneralRe: Not so sure about adding to the std namespace... how about this solution. Pin
Stuart Dootson20-Oct-09 2:12
memberStuart Dootson20-Oct-09 2:12 
Generalstd::string causes memory corruption on multi-processors PC Pin
ana_v12329-Aug-07 22:24
memberana_v12329-Aug-07 22:24 
GeneralUseful, but Pin
cozy dude15-Feb-06 1:17
membercozy dude15-Feb-06 1:17 
GeneralRe: Useful, but Pin
PJ Arends15-Feb-06 6:17
memberPJ Arends15-Feb-06 6:17 
GeneralRe: Useful, but Pin
Nishant Sivakumar15-Feb-06 16:55
staffNishant Sivakumar15-Feb-06 16:55 
GeneralRe: Useful, but Pin
David Pritchard27-Mar-06 11:27
memberDavid Pritchard27-Mar-06 11:27 
GeneralNice. Just... Pin
Nemanja Trifunovic12-Feb-06 8:38
memberNemanja Trifunovic12-Feb-06 8:38 
GeneralRe: Nice. Just... Pin
PJ Arends12-Feb-06 15:54
memberPJ Arends12-Feb-06 15:54 
GeneralRe: Nice. Just... Pin
Nemanja Trifunovic13-Feb-06 5:47
memberNemanja Trifunovic13-Feb-06 5:47 
Generaltypedef vs #define Pin
Nishant Sivakumar12-Feb-06 4:18
staffNishant Sivakumar12-Feb-06 4:18 
GeneralRe: typedef vs #define Pin
Rama Krishna Vavilala12-Feb-06 6:34
memberRama Krishna Vavilala12-Feb-06 6:34 
GeneralRe: typedef vs #define Pin
Nishant Sivakumar12-Feb-06 6:41
staffNishant Sivakumar12-Feb-06 6:41 
GeneralRe: typedef vs #define Pin
PJ Arends12-Feb-06 7:11
memberPJ Arends12-Feb-06 7:11 
GeneralRe: typedef vs #define Pin
Nishant Sivakumar12-Feb-06 9:14
staffNishant Sivakumar12-Feb-06 9:14 
Thanks PJ.

Regards,
Nish


GeneralRe: typedef vs #define Pin
Rama Krishna Vavilala12-Feb-06 7:12
memberRama Krishna Vavilala12-Feb-06 7:12 
GeneralRe: typedef vs #define Pin
Anders Dalvander12-Feb-06 22:18
memberAnders Dalvander12-Feb-06 22:18 
GeneralRe: typedef vs #define Pin
toxcct13-Feb-06 1:29
membertoxcct13-Feb-06 1:29 
GeneralRe: typedef vs #define Pin
Anders Dalvander13-Feb-06 1:35
memberAnders Dalvander13-Feb-06 1:35 
GeneralRe: typedef vs #define Pin
toxcct13-Feb-06 1:38
membertoxcct13-Feb-06 1:38 
GeneralRe: typedef vs #define Pin
PJ Arends13-Feb-06 7:43
memberPJ Arends13-Feb-06 7:43 
GeneralRe: typedef vs #define Pin
Ling, Xiao-li28-Feb-07 1:25
memberLing, Xiao-li28-Feb-07 1:25 
GeneralSome Remarks Pin
Roland Pibinger12-Feb-06 1:59
memberRoland Pibinger12-Feb-06 1:59 
GeneralRe: Some Remarks Pin
PJ Arends12-Feb-06 7:33
memberPJ Arends12-Feb-06 7:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 12 Feb 2006
Article Copyright 2006 by PJ Arends
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid