Reputation: 238471
Consider following example:
template<class CharT>
bool is_minus(CharT c) {
return c == '-';
}
//assert(is_minus('-')); // works of course
assert(is_minus(u8'-'));
assert(is_minus(u'-'));
assert(is_minus(U'-'));
assert(is_minus(L'-'));
This works on my system. Would this be guaranteed by the standard to work correctly on all systems? Considering that the character literal in the function is of different type. Is this true for other characters?
If it is not guaranteed, is there a way to write the literal in a generic manner so that I get one of '-', u8'-', u'-', U'-' or L'-'
depending on CharT
within the template.
Note: Unicode characters that could be used to represent alternative minus in some locale is outside the context of the question.
Upvotes: 1
Views: 105
Reputation: 32727
An unprefixed character is encoded based on the execution character set. L
-prefixed characters are encoded based on the execution wide-character set. The others are encoded based on the ISO 10640 code points. (See [lex.com] in the standard or on cppreference where it discusses character literals.)
If the execution character set encodes the minus character the same way as ISO 10640 you're good, but if they're different this won't work. Since most (if not all) systems use ASCII for the execution character set you're fine.
Starting in C++20, you can specialize is_minus
for each character type, and provide the properly prefixed character for each one since each prefix form has a distinct type. (This is mostly possible pre-C++20, except that u8
prefixed characters are of type char
, the same as unprefixed characters. In C++20 u8
prefixed characters are of type char8_t
.)
Upvotes: 1