Introduction
I had asked on the Visual C++ forum if there was a standard header file that would provide a way of declaring std::string
s 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 typedef
s to define new synonyms for some of the commonly used STL classes that use char
s and wchar_t
s. 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.
#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
}
Father of two, brother of two, child of two.
Spouse to one, uncle to many, friend to lots.
Farmer, carpenter, mechanic, electrician, but definitely not a plumber.
Likes walks with the wife, board games, card games, travel, and camping in the summer.
High school graduate, college drop-out.
Hobby programmer who knows C++ with MFC and the STL.
Has dabbled with BASIC, Pascal, Fortran, COBOL, C#, SQL, ASM, and HTML.
Realized long ago that programming is fun when there is nobody pressuring you with schedules and timelines.