Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 908441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:43:08+00:00 2026-05-15T16:43:08+00:00

The following compiles in Visual Studio but fails to compile under g++. int main()

  • 0

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++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:43:09+00:00Added an answer on May 15, 2026 at 4:43 pm

    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 char is not a simple-type-specifier; this is related to a question brought up by James.

    Obviously, if unsigned char was a simple-type-specifier, it would be legal. A work-around is to use std::identity:

    template <typename T>
    struct identity
    {
        typedef T type;
    };
    

    And then:

    int a = std::identity<unsigned char>::type('0');
    

    std::identity<unsigned char>::type is 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code compiles in Visual Studio 2010 but fails to compile in the
The following snippet fails to compile with Visual Studio 2010, but GCC likes it:
The following code compiles with G++ 4.6.1, but not with Visual Studio 2008 return
the following code compiles with Visual Studio 2008 but not with g++ on Mac
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
The following code compiles with gcc-4.5.1 but not in Visual Studio 11. #include <map>
The following source code compiles correctly with Visual Studio 2010: namespace NS { class
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
Visual Studio 2008 I am using the following source code that compiles ok using
The following C++ program compiles and runs as expected: #include <stdio.h> int main(int argc,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.