Home > Enterprise >  Unable to convert std::string into std::basic_string<char8_t>, why?
Unable to convert std::string into std::basic_string<char8_t>, why?

Time:09-18

I am facing the following problem, I am trying to convert an std::string object into an std::basic_string<char8_t> one, using the codecvt library. The code is the following:

#include <string>
#include <codecvt>
#include <locale>

int main()
 {
  std::string str = "Test string";
  std::wstring_convert <std::codecvt_utf8_utf16 <char8_t>, char8_t> converter_8_t;
  converter_8_t.from_bytes( str );
 }

The problem is that when I try to compile it with g -std=c 20 (g 11.2.0) I got the following error:

/usr/bin/ld: /tmp/cck8g9Wa.o: in function `std::__cxx11::wstring_convert<std::codecvt_utf8_utf16<char8_t, 1114111ul, (std::codecvt_mode)0>, char8_t, std::allocator<char8_t>, std::allocator<char> >::wstring_convert()':
other.cpp:(.text._ZNSt7__cxx1115wstring_convertISt18codecvt_utf8_utf16IDuLm1114111ELSt12codecvt_mode0EEDuSaIDuESaIcEEC2Ev[_ZNSt7__cxx1115wstring_convertISt18codecvt_utf8_utf16IDuLm1114111ELSt12codecvt_mode0EEDuSaIDuESaIcEEC5Ev] 0x2c): undefined reference to `std::codecvt_utf8_utf16<char8_t, 1114111ul, (std::codecvt_mode)0>::codecvt_utf8_utf16(unsigned long)'
collect2: error: ld returned 1 exit status

Do you know what could be the problem? Am I trying to convert the std::string object in the wrong way? Thanks.

CodePudding user response:

C keywords: char8_t (since C 20)

As far as I understand char8_t is of char type as is your std::string. A simple cast should work.

  • Related