daljinder singh
daljinder singh

Reputation: 187

Narrowing Conversion required while list initialization

I read about narrowing conversion on the cpp reference website. I kind of understood it but what i am not getting is that why is the error present only in the first line.

    long double ld = 3.1415926536;
    int a{ld}, b = {ld}; // error: narrowing conversion required
    int c(ld), d = ld;   // ok: but value will be truncated

Why is the error only present in first line and not the second?

Upvotes: 6

Views: 465

Answers (1)

vsoftco
vsoftco

Reputation: 56577

Because the compiler is required to issue a diagnostic (in your case error) for narrowing only for list initialization (a.k.a. uniform initialization), introduced starting with C++11. For the pre-C++11 initialization without curly braces, there is no diagnostic required.

See the cppreference.com documentation for more details.

Also see this answer as to why the compiler is only required to issue a warning, not necessarily an error.

Upvotes: 6

Related Questions