Calmarius
Calmarius

Reputation: 19441

How can I disable or have a warning when using wchar_t outside Windows?

The wchar_t type is used extensively on Windows API and C++ standard library APIs derived from them therefore it's hard to change Windows code to use something else because you would have to cast/convert back and forth every time.

But on non-Windows wide characters are rarely used and UTF-8 encoding is preferred instead. Therefore having code that uses wchar_t outside Windows probably does something wrong and even if its intended it's better to use types that communicate the intent better eg. using std::u16string and char16_t when dealing with UTF-16 strings instead of wstring and using std::u32string and char32_t when the intent is storing Unicode codepoints.

Is there a GCC option to turn on a diagnostic project wide that warns or errors when it sees a wchar_t, therefore identifying potential sites for refactoring?

Upvotes: 0

Views: 444

Answers (1)

RoQuOTriX
RoQuOTriX

Reputation: 3001

That is a little work around and not dedicated to GCC and also will break your build but allows you to find where you use wchar_t. (And also breaks included third-party code more or less)

You can override the definition of wchar_t with the preprocessor which then leads to errors on the usage. In that way you can find the potential usages:

#define wchar_t void

wchar_t Foo() { }

int main()
{
    auto wchar_used = Foo();
}

Error message:

error: 'void wchar_used' has incomplete type
   10 |     auto wchar_used = Foo();

Upvotes: 1

Related Questions