Title: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: Skybuck on December 25, 2020, 11:21:13 PM Example of bitcoin code in chain.h
class CBlockIndex { public: //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex const uint256* phashBlock{nullptr}; //! pointer to the index of the predecessor of this block CBlockIndex* pprev{nullptr}; //! pointer to the index of some further predecessor of this block CBlockIndex* pskip{nullptr}; //! height of the entry in the chain. The genesis block has height 0 int nHeight{0}; //! Which # file this block is stored in (blk?????.dat) int nFile{0}; //! Byte offset within blk?????.dat where this block's data is stored unsigned int nDataPos{0}; //! Byte offset within rev?????.dat where this block's undo data is stored unsigned int nUndoPos{0}; Notice these {0} What is the technical c++ term for this syntax {0} ? Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: zeuner on December 26, 2020, 12:06:08 AM This is an initializer list used for uniform initialization.
Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: NotATether on December 26, 2020, 12:30:29 AM In C++ you are able to initialize variables at compile-time using {}-initialization instead of having to assign them each time you create an object of the C++ class. It's not limited to just numbers, you can use anything like strings or floating-point numbers
So {0} just initializes the values nHeight, nFile, nDataPos and nDataPos to zero. pskip, pprev and phashBlock are initialized to nullptr (the NULL pointer that doesn't reference anything). These are also valid: Code: {"foobar"} // initialize to string or char[] You can also use {}-initialization in functions and methods. Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: Skybuck on December 26, 2020, 03:56:36 AM I find this kinda strange, when I google these technical terms the first link that shows up is:
https://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/ On the first line of text it says: "C++03 has partial support for initializer lists, allowing you to use initializer lists for simple aggregate data types (structs and C-style arrays):" Perhaps this is old, but from reading that it seems this would be limited to structs and arrays only. But now it is being applied to variables/integers and such as well ? Is this allowed in later versions of C++ ? Or is this some kind of abuse ? Are variables now considered 1 element arrays ? Does anybody have any links to "official" documentation how this feature is supposed to work if there is only one parameter as in the original posting: int nHeight{0}; Perhaps in newer versions of C++ ? From which version of C++ would this be allowed ? And is it still considered an initialization list ? While there is no real list, or an list of one element if you will ;). Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: odolvlobo on December 26, 2020, 05:31:48 AM "C++03 has partial support for initializer lists, allowing you to use initializer lists for simple aggregate data types (structs and C-style arrays):" New features and syntax have been added to C++ since then. The kind of initialization you are asking about is one of them. https://en.cppreference.com/w/cpp/language/direct_initialization Quote T object { arg }; (2) (since C++11) ... 2) initialization of an object of non-class type with a single brace-enclosed initializer (note: for class types and other uses of braced-init-list, see list-initialization) Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: NotATether on December 26, 2020, 07:21:10 AM I find this kinda strange, when I google these technical terms the first link that shows up is: https://www.learncpp.com/cpp-tutorial/b-4-initializer-lists-and-uniform-initialization/ On the first line of text it says: "C++03 has partial support for initializer lists, allowing you to use initializer lists for simple aggregate data types (structs and C-style arrays):" Perhaps this is old, but from reading that it seems this would be limited to structs and arrays only. But now it is being applied to variables/integers and such as well ? You are talking about a different feature, the one you mention in this quote is for "array initializers" introduced in C++03 which let you put series of constants inside curly braces and assign them to a static array. It is a different feature from {}-initialization which lets you assign values to variables instead of using the "=" sign. And introduced in C++11. Is this allowed in later versions of C++ ? Or is this some kind of abuse ? Yes it's allowed. All amendments to the C++ standard are forward compatible except in the few cases where they explicitly state they are removing features. Are variables now considered 1 element arrays ? No, see my response above. Does anybody have any links to "official" documentation how this feature is supposed to work if there is only one parameter as in the original posting: int nHeight{0}; Perhaps in newer versions of C++ ? From which version of C++ would this be allowed ? And is it still considered an initialization list ? While there is no real list, or an list of one element if you will ;). Again, this is {}-initialization so the value inside braces doesn't have to be an array. What you just wrote is equivalent to: Code: int nHeight = 0; Basically when we see a variable name immediately followed by brackets, the compiler removes the outer brackets and changes the expression to "= inside-bracket-value". And when someone wants to use this feature to initialize an array, they can use { {element1, element2, etc. } } Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: Skybuck on December 27, 2020, 08:03:34 AM OK,
Thank you all, Last one to explain the differences. The one before last how this is called technically. "Direct initialization". Cause I was quite confused about what kind of initialization this is supposed to be and how it's technically called :) The array initialization with this seems a bit weird now. So is this a technical "break" from previous C++ versions ? So if a previous C++ version wrote: Array { 1,2,3,4,5 } That code now needs to be updated to: Array { { 1,2,3,4,5 } } ? I will keep an eye out for this, however the examples on the link https://en.cppreference.com/w/cpp/language/direct_initialization do not show this kind of array initialization ? (Perhaps to hide that this is now broken with previous C++ version ? ;) :)) Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: odolvlobo on December 27, 2020, 08:29:41 AM ... The array initialization with this seems a bit weird now. So is this a technical "break" from previous C++ versions ? So if a previous C++ version wrote: Array { 1,2,3,4,5 } ... It will not compile with an earlier version of C++. See https://en.cppreference.com/w/cpp/language/aggregate_initialization New syntax was added as a way to unify the syntax for the various methods of initialization. Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: Skybuck on January 01, 2021, 01:28:50 PM ... The array initialization with this seems a bit weird now. So is this a technical "break" from previous C++ versions ? So if a previous C++ version wrote: Array { 1,2,3,4,5 } ... It will not compile with an earlier version of C++. See https://en.cppreference.com/w/cpp/language/aggregate_initialization New syntax was added as a way to unify the syntax for the various methods of initialization. This is confusing, let's try and shine some light on this mess, do you agree with the following: Old code: Array { 1,2,3,4,5 } New code: Array { { 1,2,3,4,5 } } ? Now your post seems to indicate that the old code won't compile with an old compiler ? That does not make sense. (Then again it might make some sense if the c++ compiler is old enough. I tried it somewhat on VS 2010 and indeed the old code also didn't compile, which is a bit weird but ok, so now so many versions of c/c++ language... yikes) Perhaps you ment the new code will not compile with an older compiler. So looking at that messing link/document again I think I now understand: Every C/C++ compiler before 2011: Code: did not exist After 2011: Old code: Array { 1,2,3,4,5 } After 2020: New code: Array { { 1,2,3,4,5 } } But now old code don't compile anymore as well ? So no backwards compatibility... This feature seems a bit rushed but at least there is some progress being made in c/c++ but at the cost of confusion and having to change code, something I don't like in general. Was it worth it for you c/c++ programmers ? Looking at how does structs are initialized in those code examples, at first glance looks nice, quick, fast, lazy, but I can see that breaking real easy or causing more confusion... (for example field added to structure or re-ordering of fields, this would go undetected) For now seems to me like a feature that introduces more pain, troubles and unnecessary code than was worth it, anybody disagree ? ;) :) Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: NotATether on January 01, 2021, 03:24:02 PM This is confusing, let's try and shine some light on this mess, do you agree with the following: Old code: Array { 1,2,3,4,5 } New code: Array { { 1,2,3,4,5 } } There is actually a mistake in the old code which you wrote, it should be Array = { 1, 2, 3, 4, 5 } (the equals sign is important in the old syntax, and commas between elements are required whether you're writing old or new syntax) Now your post seems to indicate that the old code won't compile with an old compiler ? That does not make sense. (Then again it might make some sense if the c++ compiler is old enough. I tried it somewhat on VS 2010 and indeed the old code also didn't compile, which is a bit weird but ok, so now so many versions of c/c++ language... yikes) Perhaps you ment the new code will not compile with an older compiler. Your last sentence is correct, the old code will compile in both old and new versions of the compiler while the new syntax only compiled in newer compilers. In particular, you can still write Array = { 1, 2, 3, 4, 5 } in C++11 and newer. It is still valid syntax. Every C/C++ compiler before 2011: Code: did not exist After 2011: Old code: Array { 1,2,3,4,5 } After 2020: New code: Array { { 1,2,3,4,5 } } This syntax did not change at all in 2020 so I'm not sure why you listed that. This is how it actually is: Quote Every C/C++ compiler before 2011: Code: Array = { 1, 2, 3, 4, 5 } After 2011: Old code: Array = { 1,2,3,4,5 } // this still works and Array{ {1, 2, 3, 4, 5} } Was it worth it for you c/c++ programmers ? Looking at how does structs are initialized in those code examples, at first glance looks nice, quick, fast, lazy, but I can see that breaking real easy or causing more confusion... (for example field added to structure or re-ordering of fields, this would go undetected) For now seems to me like a feature that introduces more pain, troubles and unnecessary code than was worth it, anybody disagree ? ;) :) Personally I only use the new syntax if I am not initializing an array. In other words, not Array{ {1, 2, 3, 4, 5} } but I will happily write things like my_string{"Control Message"} or flag{0x001}. I like using the new syntax when I'm initializing class variables. Now I'm usually making pointers as class members and not fixed-sized arrays in classes so I have no need for Array = { 1, 2, 3, 4, 5 } inside a class but I also cannot use malloc() unless it's inside a function, so Dynamic_array { malloc(1*sizeof(Dynamic_array) } is invalid in the part of the class that's outside a function (and so is the equivalent using = sign instead of outer brackets). Title: Re: What is technical c++ term for {0} in this: int nHeight{0}; ? Post by: odolvlobo on January 02, 2021, 01:44:46 AM Old code: Array { 1,2,3,4,5 } New code: Array { { 1,2,3,4,5 } } Every C/C++ compiler before 2011: Code: Array = { 1, 2, 3, 4, 5 } After 2011: Old code: Array = { 1,2,3,4,5 } // this still works and Array{ {1, 2, 3, 4, 5} } None of those are examples of actual code, so it is impossible to comment on what is syntactically correct and what isn't. |