dimanche 12 février 2023

Does the conversions between SQLWCHAR and char16_t are safe?

I'm using the same code, using ODBC, on both Windows and Linux (linuxODBC).

On windows moving from SQLWCHAR and std::wstring is natural. bot on Linux I must convert encoding from std::wstring (UTF-32) to SQLWCHAR (UTF-16) and vice versa, and store results in std::u16string.

Whenever I give SQLWCHAR to char16_t and vice versa, I get a compilation error, which I can easily resolve with casting. using macros like this:

#ifdef WIN32
#define SQL(X) (X)
#define CHAR16(X) (X)
#else
#define SQL(X) ((const SQLWCHAR*)X)
#define CHAR16(X) ((const char16_t*)X)
#endif

both appear to have the same size, but see this in cppreference:

int_least8_t int_least16_t int_least32_t int_least64_t smallest signed integer type with width of at least 8, 16, 32 and 64 bits respectively (typedef)

the "at least" made me start to be worried...

So I would like to ask, is this casting safe?

Aucun commentaire:

Enregistrer un commentaire