Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deprecated use of std::char_traits<unsigned short>::length #791

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Clement-Devevey
Copy link

@Clement-Devevey Clement-Devevey 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]
@Clement-Devevey
Copy link
Author

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 *'

@Clement-Devevey
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants