The following compiles in Visual Studio but fails to compile under g++.
int main()
{
int a = unsigned char('0');
return 0;
}
Is unsigned char() a valid way to cast in C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it’s not legal.
A function-style explicit type conversion requires a simple-type-specifier, followed by a parenthesized expression-list. (§5.2.3)
unsigned charis not a simple-type-specifier; this is related to a question brought up by James.Obviously, if
unsigned charwas a simple-type-specifier, it would be legal. A work-around is to usestd::identity:And then:
std::identity<unsigned char>::typeis a simple-type-specifier, and its type is simply the type of the template parameter.Of course, you get a two-for-one with
static_cast. This is the preferred casting method anyway.