Is narrowing the list initialization conversion an error or just a warning?

Currently, I was a self-learning C ++ primer 5th edition. The text says:

When used with built-in type variables, this form of initialization has one Important property: the compiler will not allow us to list the initialization of built-in type variables if the initializer can lead to loss of information:

Here is a sample code:

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

But when I tried it myself on the C ++ shell:

long double a=3.14159265354;
 int b(a);
 int c{a};
 std::cout<<a<<std::endl<<b<<std::endl<<c<<std::endl;

It just gives a warning -Wnarrowing, but the program completed successfully. What for?

+4
source share
1 answer

, , . .

.

1.3.6 [defns.diagnostic]

+1

Source: https://habr.com/ru/post/1621297/


All Articles