0

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.

Gianluca Bianco
  • 656
  • 2
  • 11
  • 3
    *Do you know what could be the problem?* -- That is a linker error, not a compiler error. Your code compiled just fine -- it probably has something to do with the libraries you are linking to. Unfortunately I do not know g++ suite well enough to tell you the issue with the libraries you are using. – PaulMcKenzie Sep 17 '22 at 10:41
  • 1
    Why are you using `std::codecvt_utf8_utf16 `? Where is UTF-16 involved at all? For that matter, why do you think you need to transcode into UTF-8? What encoding is the original string in if not already UTF-8? *Such* an XY problem..! It seems to me you just need to call `std::u8string`'s iterator-pair constructor from the original string's `begin`/`end`, but as it's not clear what this is supposed to accomplish, who can really say? – ildjarn Sep 17 '22 at 19:49

1 Answers1

0

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.

BeErikk
  • 74
  • 1
  • 7
  • The OP's code compiled as-is with no issues, so no cast should be necessary. Not saying you are wrong, but the compiler *should* have issued some sort of diagnostic if a cast was really necessary. – PaulMcKenzie Sep 17 '22 at 10:42
  • 2
    Again as far as I understand OP is using a wide string converter and the type used is narrow. – BeErikk Sep 17 '22 at 12:14
  • 1
    *"A simple cast should work."* -- could you be more specific? A cast of what to what? I would consider `static_cast(&str)` to be a simple cast, but I don't see it making the question's code work. *One approach: could you add a line of code to illustrate what you described in text?* – JaMiT Sep 17 '22 at 14:39
  • &JaMit "_could you be more specific?_" - No, again as far as I understand I wouldn't. [Is C++20 'char8_t' the same as our old 'char'?](https://stackoverflow.com/questions/57402464/is-c20-char8-t-the-same-as-our-old-char/57453713#57453713) – BeErikk Sep 18 '22 at 19:29