0

In C++ Primer 2.2.1 Variable Definitions chapter They said initialize with a curly brace is called list initialize and they said

When used with variables of built-in type, this form of initialization has one important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss of information:

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

So I thought this is the difference between list initialization and other initializations. But when I tried to check it. I saw that it is working fine! And only they give some warnings(They said error). So I didn't understand anything from this. So can someone tell me what is the difference between normal initializations and list initialization?

xskxzr
  • 12,442
  • 12
  • 37
  • 77
  • 1
    It provides an error for me. What compiler are you using, with what settings? – Nathan Pierson Jul 12 '21 at 14:43
  • Depends on the compiler and warning levels, but narrowing conversions may be treated as a warning or an error. For example GCC has [`-Wconversion`](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html). Visual Studio has compiler warning [C4838](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4838?view=msvc-160) – Cory Kramer Jul 12 '21 at 14:45
  • 3
    And even if it only produces warnings, that's also good enough. Experienced, skilled C++ developers always compile their code with as many warning options enabled as possible, as well as an additional option that treats all warnings as fatal errors. – Sam Varshavchik Jul 12 '21 at 14:46
  • Mingw-w64 from ```https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download``` btw i want gcc 10 from mingw but i can't get it from msys2 and cygwin because msys2 require 64 bit and cygwin is not working when try to debug with vscode so can someone tell me where to get install latest mingw? (my pc is 32 bit) –  Jul 12 '21 at 14:47
  • oh yes, compiler flags are g++ -std=c++0x -Wall filename -o filenamewithoutextention.exe –  Jul 12 '21 at 15:24
  • @rafid100 You should add `-Werror` and its nice to use `-Wextra` – Martin York Jul 12 '21 at 16:06
  • But C++ Primer didn't explain it >_< . Btw thanks this is working after adding the flags :-) –  Jul 12 '21 at 16:21
  • Btw, @Martin York do you mean that if I don't use -Werror and -Wextra it always gives a warning on all computers not only mine? or do you mean all don't have the problem only I have the problem? if ONLY I have the problem then why? –  Jul 12 '21 at 17:13
  • @rafid100 I am suggesting you should add those extra flags when compiling code as they will reduce errors overall. – Martin York Jul 12 '21 at 17:44
  • Omg, I found another question almost the same as mine in this link ```https://stackoverflow.com/questions/34418485/narrowing-conversion-of-list-initialization-is-an-error-or-just-a-warning``` what to do? –  Jul 13 '21 at 03:19

0 Answers0