Skip to content

Conversation

kekopom
Copy link

@kekopom kekopom commented Jan 20, 2025

Fix the warning about deprecated use of char_traits by char_traits<char16_t> as it has been discussing in this thread:
@codeinred Since we've decided to make C++11 a requirement for future versions of JavaCPP, let's just replace unsigned short with char16_t. Thanks!

Originally posted by @saudet in #753 (comment)

--
Original issue is;
warning: 'char_traits' is deprecated: char_traits for T not equal to char, wchar_t, char8_t, char16_t or char32_t is non-standard and is provided for a temporary period. It will be removed in LLVM 19, so please migrate off of it. [-Wdeprecated-declarations]

@codeinred Since we've decided to make C++11 a requirement for future versions of JavaCPP, let's just replace unsigned short with char16_t. Thanks!

_Originally posted by @saudet in bytedeco#753 (comment)

warning: 'char_traits<unsigned short>' is deprecated: char_traits<T> for T not equal to char, wchar_t, char8_t, char16_t or char32_t is non-standard and is provided for a temporary period. It will be removed in LLVM 19, so please migrate off of it. [-Wdeprecated-declarations]
@kekopom
Copy link
Author

kekopom commented Jan 20, 2025

Having one issue about doing this:
in JavaCPP_createStringFromUTF16 the return env->NewString(ptr, length); isn't working:
Cannot initialize a parameter of type 'const jchar *' (aka 'const unsigned short *') with an lvalue of type 'const char16_t *'

@kekopom
Copy link
Author

kekopom commented Jan 22, 2025

Apparently this is safe to cast from char16_t to jchar

It is generally safe to cast char16_t* to jchar* in JNI, as both types are 16-bit wide and represent UTF-16 encoded characters. This approach is commonly used in JNI code to handle UTF-16 strings.

Here’s a brief explanation:

char16_t: A 16-bit character type introduced in C++11 to represent UTF-16 encoded characters.
jchar: A 16-bit type used by JNI to represent UTF-16 encoded characters, which is typically defined as unsigned short.
Since both char16_t and jchar are 16-bit wide and represent UTF-16 characters, casting between them is safe and does not alter the data. This practice is used in various JNI implementations.µ

--

Using reinterpret_cast ensures that the conversion is done correctly at the bit level, which is necessary for this specific case in JNI where char16_t and jchar are essentially the same type but with different names.

@saudet
Copy link
Member

saudet commented Jan 23, 2025

There a few other places where we could switch from unsigned short to char16_t, so we might as well do it now?

@saudet
Copy link
Member

saudet commented Jan 30, 2025

It looks like it doesn't work at all if we don't change all unsigned short to char16_t, so please do that! Thanks

@saudet
Copy link
Member

saudet commented Mar 15, 2025

Please fix the builds

@saudet saudet removed the help wanted label Apr 9, 2025
@saudet
Copy link
Member

saudet commented Apr 9, 2025

Superseded by #800

@saudet saudet closed this Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants