mardi 31 janvier 2023

Why trying to print unicode encoded strings with cout leads to compilation error in newer C++ standards?

I tried the following printing of Unicode characters with Visual C++ 2022 Version 17.4.4 with C++ standard set to the latest.

#include <iostream>

using namespace std;

int main()
{
  cout << u8"The official vowels in Danish are: a, e, i, o, u, \u00E6, \u00F8, \u00E5 and y.\n";
  return 0;
}

I have the compilation error:

1>C:\projects\cpp\test\test.cpp(7,8): error C2280: 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char8_t *)': attempting to reference a deleted function
1>C:\projects\cpp\test\test.cpp(7,8): error C2088: '<<': illegal for class

The same behavior is observed with u (utf-16) and U (utf-32) string literals.

Setting the standard to C++17 or C++14 makes the program to compile.

What is the rationale for disallowing this code in C++20 and later standards and what is the correct way to print Unicode string literals in those standards?

Aucun commentaire:

Enregistrer un commentaire