Narrowing conversion required when initializing a list

I read about narrowing conversions on cpp links site. I kind of figured it out, but what I'm not getting is why the error is only present on 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 on the first line and not on the second?

+3


source to share


1 answer


Since the compiler must issue a diagnostic (in your error) to narrow only to the list initialization (aka uniform initialization ), introduced starting with C ++ 11. To initialize the pre-C ++ 11 without braces diagnosis is not required.

See cppreference.com documentation for details .



Also see this answer for why the compiler should only issue a warning and not necessarily an error.

+3


source







All Articles