What Does _T Stands for in a Cstring

What does _T stands for in a CString

_T stands for “text”. It will turn your literal into a Unicode wide character literal if and only if you are compiling your sources with Unicode support. See http://msdn.microsoft.com/en-us/library/c426s321.aspx.

What is the meaning of _T(xyz)?

You probably want this:

uint32_t xyz = 15;
LPCWSTR Desc = L"xyz value is : %d";
CString Value;
Value.Format(Desc, xyz);

Forget about the _T macro. What makes you think you should use it?

Or maybe you need this:

uint32_t xyz = 15;                // integer variable containing the number 15
CString stxyz; // CString variable
stxyz.Format(L"%d", xyz); // stxyz contains "15" now

LPCWSTR Desc = L"xyz value is : "; // Desc points to the string literal "15"

CString Value = Desc + stxyz; // Value contains concatenation of Desc and stxyz

Should I use _T or _TEXT on C++ string literals?

A simple grep of the SDK shows us that the answer is that it doesn't matter—they are the same. They both turn into __T(x).


C:\...\Visual Studio 8\VC>findstr /spin /c:"#define _T(" *.h
crt\src\tchar.h:2439:#define _T(x) __T(x)
include\tchar.h:2390:#define _T(x) __T(x)

C:\...\Visual Studio 8\VC>findstr /spin /c:"#define _TEXT(" *.h
crt\src\tchar.h:2440:#define _TEXT(x) __T(x)
include\tchar.h:2391:#define _TEXT(x) __T(x)

And for completeness:


C:\...\Visual Studio 8\VC>findstr /spin /c:"#define __T(" *.h
crt\src\tchar.h:210:#define __T(x) L ## x
crt\src\tchar.h:889:#define __T(x) x
include\tchar.h:210:#define __T(x) L ## x
include\tchar.h:858:#define __T(x) x

However, technically, for C++ you should be using TEXT() instead of _TEXT(), but it (eventually) expands to the same thing too.

Using the value _T() and CString variables

CString has different constructors and operator overloads, it is prepared for different scenarios. It's a complicated class but in general, such classes work like this:

Use default constructor CString() (initializes for _T("")):

CString s1 = CString();     
CString s2; //same as above, shortcut
CString s3{}; //same as above
CString s4 = {}; //same as above

Use copy constructor CString(const CString& src):

const CString src = _T("");

CString s1 = CString(src);
CString s2 = src; //same as above
CString s3 = { src}; //same as above
CString s4(src); //same as above

Use different constructor CString(const TCHAR* src):

const TCHAR* src = _T("");

CString s1 = CString(src);
CString s2 = src; //same as above
CString s3 = { src }; //same as above
CString s4(src); //same as above

CString also supports = assignment operator:

CString s;
s = _T(""); //uses `=` assignment operator

s = CString(); //uses default constructor for right side,
//calls `=` assignment operator for left side.

== operator overload may use CString::Compare to compare the two sides. So

if (s == _T("")) is same as if (s.Compare(_T("")) == 0), it might be 1 nanosecond slower than s.IsEmpty(), otherwise is valid.

How to use a variable inside a _T wrapper?

It doesn't work because the _T() macro works only with constant string literals. The definition for _T() looks something like this:

#ifdef UNICODE
#define _T(str) L##str
#else
#define _T(str) str

Since you're apparently compiling in Unicode mode, _T(full) expands to Lfull, which is obviously not what you want.

In your case, just pass in full without the _T() macro since CString defines a conversion operator to a const wchar_t* in Unicode mode, and const char* in non-Unicode mode.

ShellExecute(0, _T("open"), _T("c:\\IECapt"), full, 0, SW_HIDE);

Note that standard C++ also provides a std::string type and a std::wstring type which does pretty much what CString does, so MFC isn't actually required for string manipulation. std::string does not provide a conversion operator, but does provide access to the underlying C-style string via c_str().

Trim / remove a tab ( \t ) from a string

hackingwords' answer gets you halfway there. But std::remove() from <algorithm> doesn't actually make the string any shorter -- it just returns an iterator saying "the new sequence would end here." You need to call my_string().erase() to do that:

#include <string>
#include <algorithm> // For std::remove()

my_str.erase(std::remove(my_str.begin(), my_str.end(), '\t'), my_str.end());

What is size_t in C?

From Wikipedia:

According to the 1999 ISO C standard
(C99), size_t is an unsigned integer
type of at least 16 bit (see sections
7.17 and 7.18.3).

size_tis an unsigned data type
defined by several C/C++ standards,
e.g. the C99 ISO/IEC 9899 standard,
that is defined in stddef.h.1 It can
be further imported by inclusion of
stdlib.h as this file internally sub
includes stddef.h.

This type is used to represent the
size of an object. Library functions
that take or return sizes expect them
to be of type or have the return type
of size_t. Further, the most
frequently used compiler-based
operator sizeof should evaluate to a
constant value that is compatible with
size_t.

As an implication, size_t is a type guaranteed to hold any array index.



Related Topics



Leave a reply



Submit