c++unicodefmt

Display unicode character in fmt lib C++


I want to display the infinity symbol which has Unicode U+221E. I am currently using the fmt library, it is supposed to have a lot of support and be cross-platform.

fmt::print("", fmt::styled("∞ >", fmt::emphasis::bold | fg(fmt::color::aquamarine)));

I get the following output:

? >

I also tried setting: setlocale(LC_ALL, "en_US.UTF-8"); doesn't help. I am on Windows 11 x64.

WARNING:

warning C4566: character represented by universal-character-name '\u221E' cannot be represented in the current code page (1252)

MS Visual Studio 2022 IDE.

Should I change the Character Set, in project properties? Currently set to: Use Unicode Character Set, second option is: Use Multi-Byte Character Set.


Solution

  • For this to work with MSVC, you need to compile with the /utf-8 flag. Among other things, this sets the string literal encoding to UTF-8. It is detected by {fmt}, which uses Unicode APIs to write to a console. Changing the locale won't help in general, because the problems are elsewhere:

    1. Your string literal encoding (sometimes known as the execution encoding) doesn't match your source encoding - this is what causing the warning.
    2. Console uses a separate codepage (encoding).

    Using wide strings won't help either, for similar reasons.