0

There is the function wcsncat_s() for concatenating two wchar_t*:

errno_t wcsncat_s( wchar_t *restrict dest, rsize_t destsz, const wchar_t *restrict src, rsize_t count );

Is there an equivalent function for concatenating two char16_t?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user3443063
  • 1,455
  • 4
  • 23
  • 37
  • Short answer: No. Long answer: No, but you can write one yourself. – Adrian Mole Mar 01 '22 at 16:23
  • 1
    Concatenate two individual `char16_t` characters, or two `char16_t*` strings? Why are you not using `std::u16string`? Then you can use its `operator+` – Remy Lebeau Mar 01 '22 at 16:25
  • Why tag with `C++` if you are looking for `C` function? As Remy point out in C++ just use `std::u16string`. – Marek R Mar 01 '22 at 16:31

2 Answers2

2

Not really.

On Windows, though, wchar_t is functionally identical to char16_t, so you could just cast your char16_t* to a wchar_t*.

Otherwise you can do it simply enough by writing yourself a function for it.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39
2

You could use std::u16string if you want something portable.

std::u16string str1(u16"The quick brown fox ");
std::u16string str2(u16"Jumped over the lazy dog");

std::u16string str3 = str1+str2;  // concatenate

const char16_t* psz = str3.c_str();

The validity of psz lasts as long as str3 doesn't go out of out scope.

But the more portable and flexible solution is to just use wchar_t everywhere (which is 32-bit on Mac). Unless you are explicitly using 16-bit char strings (perhaps for a specific UTf16 processing routine), it's easier to just keep your code in the wide char (wchar_t) space. Plays nicer with native APIs and libraries on Mac and Windows.

selbie
  • 100,020
  • 15
  • 103
  • 173