Re:The C++20 Naughty and Nice List for Game Devs
by pjmlp on Monday December 25, 2023 @10:11AM
(#38763226)
Contrary to C, in C++ initialization order of data members matters and can have side effects, that is why.
[ Parent
| on HN ]
Re:The C++20 Naughty and Nice List for Game Devs
by rwmj on Monday December 25, 2023 @11:46AM
(#38763999)
I think an exception might be made for a plain "C-like" struct that doesn't initialize members or contain anything except basic types. In the specific example[0] the code is actually surrounded by extern "C" { ... } so I suppose that the compiler "knows" this is a plain C struct? (Does extern "C" change parsing rules? I will need to look at what GCC does)
[0] https://gitlab.com/nbdkit/nbdkit/-/blob/cd761c9bf770b23f678f...
Re:The C++20 Naughty and Nice List for Game Devs
by loeg on Monday December 25, 2023 @12:28PM
(#38764371)
C++ compilers know when a structure is POD (plain old data) and could relax initialization order rules for them. I doubt they will.
Re:The C++20 Naughty and Nice List for Game Devs
by pjmlp on Monday December 25, 2023 @12:39PM
(#38764442)
And suddenly, after a small struct field change that invalidates POD rules, the code stops compiling, for example.
Re:The C++20 Naughty and Nice List for Game Devs
by loeg on Monday December 25, 2023 @12:56PM
(#38764588)
Sure, although making small edits thay break compilation isn’t all that unusual in C++. Eg, deleted constructors in added members, etc.
Re:The C++20 Naughty and Nice List for Game Devs
by flohofwoe on Monday December 25, 2023 @01:42PM
(#38764935)
extern "C" { ... } unfortunately only turns off C++ name mangling for symbols, it doesn't switch the C++ compiler into "C language mode". Such a feature would be incredibly useful though for mixing C and C++ code in the same source file.
Re:The C++20 Naughty and Nice List for Game Devs
by pjmlp on Monday December 25, 2023 @02:38PM
(#38765444)
Naturally, that was always its goal, extern "language" {} produces the same linkage model as language.
Even though many only know it with C, Fortran, Pascal and Ada were also common when C++ was gaining adoption.
It was never about being a polyglot compiler.