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 6979803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:55:57+00:00 2026-05-27T17:55:57+00:00

Since switching on the C++0x standard in g++, I’ve started seeing ‘narrowing conversion’ errors,

  • 0

Since switching on the C++0x standard in g++, I’ve started seeing ‘narrowing conversion’ errors, particularly when converting from an ‘int’ to a ‘short’ although I understand the error covers a much broader swath of conversions.

Can anyone shed some light on the rational for introducing this extra level of safety?, What are the possible consequences of disabling this error? (apart from the potential loss of precision).

Thanks.

  • 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-27T17:55:58+00:00Added an answer on May 27, 2026 at 5:55 pm

    From Assignment and compound assignment operators [expr.ass]

    The
    meaning of x={v}, where T is the scalar type of the expression x, is that of x=T(v) except that no
    narrowing conversion (8.5.4) is allowed.

    and from List-initialization [dcl.ini.list]

    If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed.

    So basically you can’t ignore it, your program is ill-formed in presence of narrowing conversions.

    From Implementation compliance:

    Implementations are required to diagnose programs that
    use such extensions that are ill-formed according to this International Standard. Having done so, however, they can compile and execute such programs.

    Bjarne Stroustroup say this:

    Preventing narrowing

    The problem: C and C++ implicitly truncates:

       int x = 7.3;        // Ouch!
        void f(int);
        f(7.3);         // Ouch!

    However, in C++0x, {} initialization doesn’t narrow:

    int x0 {7.3};   // error: narrowing
    int x1 = {7.3}; // error: narrowing
    double d = 7;
    int x2{d};      // error: narrowing (double to int)
    char x3{7};     // ok: even though 7 is an int, this is not narrowing
    vector<int> vi = { 1, 2.3, 4, 5.6 };    // error: double to int narrowing
    

    The way C++0x avoids a lot of incompatibilities is by relying on the actual values of initializers (such as 7 in the example above) when it can (and not just type) when deciding what is a narrowing conversion. If a value can be represented exactly as the target type, the conversion is not narrowing.

    char c1{7};      // OK: 7 is an int, but it fits in a char
    char c2{77777};  // error: narrowing 
    

    Note that floating-point to integer conversions are always considered narrowing — even 7.0 to 7.

    So in a way, narrowing also increases type safety.

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

Sidebar

Related Questions

Ever since switching from TABLE-layout to DIV-layout, one common problem remains: PROBLEM : you
I started switching some pre-existing nHibernate code in a Sharepoint-based ASP.NET project from eager
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
Since Graduating from a very small school in 2006 with a badly shaped &
I'm having a problem with data entry since switching to .NET 4.0. In my
I’m switching from Notepad++ to Vim as my main text editor. In Notepad++, you
I am just switching from C to C# and would like to invest sometime
I started implementing something similar to task switching in my app on atmega8. The
This post is a duplicate from superuser.com, but since I had no answers, I
Since switching to Windows 7, we've noticed that several of our web pages have

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.