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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:38:28+00:00 2026-05-27T01:38:28+00:00

The concept of narrowing seems pretty straight-forward. However, could someone please explain why some

  • 0

The concept of narrowing seems pretty straight-forward. However, could someone please explain why some of the code below causes “narrowing” compiler errors and others don’t?

This code produces errors where expected:

constexpr int a = 255;
unsigned char b = a;      // OK
unsigned char c = a + 1;  // Error... expected

This code doesn’t produce errors, but may be ok:

int d = 256;
unsigned char e = d;  // Maybe OK because 'd' is not constexpr

This code should generate errors (unless I’m missing something):

int f = 42.0;  // Maybe OK because no fractional part
int g = 42.1;  // OK... should fail!!
constexpr float h = 42.7;
int i = h;     // OK... should fail???

I’m using g++ 4.6.2. I searched the GCC bug database and didn’t find anything related. 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-27T01:38:29+00:00Added an answer on May 27, 2026 at 1:38 am

    To be honest, with your samples I see little wrong.

    However, there are a number of cases where the compiler seems to accept ‘violations’ of the standard conversion rules…:

    Initializer lists (§ 8.5.4)

    However I spotted this one in the standard:

    For initialzer lists, the following is not allowed (§ 8.5.4, under 3.)

    int ai[] = { 1, 2.0 }; // error narrowing
    

    Under 6. it goes on to give a general list of examples:

    [ Note: As indicated above, such conversions are not allowed at the top level in list-initializations.—end
    note ]

    int x = 999; // x is not a constant expression
    const int y = 999;
    const int z = 99;
    char c1 = x; // OK, though it might narrow (in this case, it does narrow)
    char c2{x}; // error: might narrow
    char c3{y}; // error: narrows (assuming char is 8 bits)
    char c4{z}; // OK: no narrowing needed
    unsigned char uc1 = {5}; // OK: no narrowing needed
    unsigned char uc2 = {-1}; // error: narrows
    unsigned int ui1 = {-1}; // error: narrows
    signed int si1 =
    { (unsigned int)-1 }; // error: narrows
    int ii = {2.0}; // error: narrows
    float f1 { x }; // error: might narrow
    float f2 { 7 }; // OK: 7 can be exactly represented as a float
    int f(int);
    int a[] = { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level
    

    Interestingly, g++ 4.6.1 with --std=c++0x -Wall -pedantic catches only one of these violations:

        char c3{y}; // warning: overflow in implicit constant conversion [-Woverflow]
    

    Outside initializer lists…

    I don’t think the truncation of a float to an int is considered narrowing.

    It is just a well-defined conversion, much like

    int i = 31;
    i /= 4;   // well defined loss of precision...   
    i /= 4.0; // equally well-defined conversion from floating point to int
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This concept seems to trouble me. Why does an NSError object need its pointer
Concept: Drinks are made of components. E.g. 10ml of Vodka. In some receipt the
The concept is this, You place JS Code for a video in a div,
The concept of this seems easy, but I'm having trouble getting it right and
Can anyone explain the concept of covariance and contravariance in programming language theory?
Simple concept, however it's not working. I have two buttons in my UIWebView ,
Apache Felix has the concept of an extension bundle. This seems to be a
We have the concept of pointers in C++. Now if we allocate some memory
Concept So, I'm trying to define a macro to simplify the following code: for
The concept is pretty simple actually, and involves finding out the hash function from

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.