Download

Reasons for sticking with TCHAR?

Why Unreal Engine 4 still uses TCHAR? What are the benefits not using wchar_t (if implementation and source uses Unicode, it still can be UTF16LE/BE or UTF32 depending on platform), or char with UTF8 literals? Historical reasons or do I miss important things?

I’m asking b/c using UE4 it seems to me that I can’t be sure to know which type of character I’m facing, so interfacing FStrings e.g. with the C++ standard library is annoying - it can be char or wchar_t depending on how the TEXT() macro is resolved. I assume it is mostly wchar_t since L is prepended as default in Platform.h:



// If we don't have a platform-specific define for the TEXT macro, define it now.
#if !defined(TEXT) && !UE_BUILD_DOCS
	#define TEXT_PASTE(x) L ## x
	#define TEXT(x) TEXT_PASTE(x)
#endif


There are two reasons: 1) to have control over the actual size of the type; 2) to make this size to be the same across all platforms. As far as I know, sizeof(wchar_t) is implementation-dependent by the Standard. Your code should never assume the size of TCHAR. I suggest using FString where possible and using FCString family of functions instead of the Standard Library functions when using TCHAR.

I see that it is required to have control over the actual size of the type, especially when dealing with Unicode and yes, wchar_t can have different sizes depending on implementation. So wchar_t is (and should be) not an option. However, C++ has fixed width types char, char16_t, char32_t. From my understanding it is easier if a (multiplatform) program sticks to one type instead of a “dynamic width type” since it eases interface design (the conversion target, e.g. UTF8 char -> UTF32 char32_t, is known independently from the implementation).

Yes, when staying inside UE4 I try to stick to FString only.

As far as I know, older versions of the Unreal Engine allowed you to choose between ANSI build mode and Unicode build mode with 8-bit and 16-bit TCHAR respectively. And if you were using TCHAR you were sure that your code can be compiled in both modes. Maybe future versions of the Engine will allow you to choose between, say, 16-bit TCHAR and 32-bit TCHAR, who knows. :slight_smile: And by using TCHAR you make sure that your code will work.