Bitcoin Forum
May 10, 2024, 06:22:38 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: What is technical c++ term for {0} in this: int nHeight{0}; ?  (Read 166 times)
Skybuck (OP)
Full Member
***
Offline Offline

Activity: 384
Merit: 110


View Profile
December 25, 2020, 11:21:13 PM
 #1

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?Huh?.dat)
    int nFile{0};

    //! Byte offset within blk?Huh?.dat where this block's data is stored
    unsigned int nDataPos{0};

    //! Byte offset within rev?Huh?.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} ?
1715365358
Hero Member
*
Offline Offline

Posts: 1715365358

View Profile Personal Message (Offline)

Ignore
1715365358
Reply with quote  #2

1715365358
Report to moderator
1715365358
Hero Member
*
Offline Offline

Posts: 1715365358

View Profile Personal Message (Offline)

Ignore
1715365358
Reply with quote  #2

1715365358
Report to moderator
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715365358
Hero Member
*
Offline Offline

Posts: 1715365358

View Profile Personal Message (Offline)

Ignore
1715365358
Reply with quote  #2

1715365358
Report to moderator
1715365358
Hero Member
*
Offline Offline

Posts: 1715365358

View Profile Personal Message (Offline)

Ignore
1715365358
Reply with quote  #2

1715365358
Report to moderator
1715365358
Hero Member
*
Offline Offline

Posts: 1715365358

View Profile Personal Message (Offline)

Ignore
1715365358
Reply with quote  #2

1715365358
Report to moderator
zeuner
Member
**
Offline Offline

Activity: 189
Merit: 16


View Profile
December 26, 2020, 12:06:08 AM
 #2

This is an initializer list used for uniform initialization.
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6735


bitcoincleanup.com / bitmixlist.org


View Profile WWW
December 26, 2020, 12:30:29 AM
Last edit: December 26, 2020, 07:23:07 AM by NotATether
Merited by Heisenberg_Hunter (1)
 #3

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 as long as you only use constants. Variables are not allowed between the braces (I was wrong about this part, you can use any variable in {}-initialization).

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[]
{3.14}      // initialize to float or double
{ {1,2,3} } // initialize to int[] array (inner curly braces are for initializing array)

You can also use {}-initialization in functions and methods.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Skybuck (OP)
Full Member
***
Offline Offline

Activity: 384
Merit: 110


View Profile
December 26, 2020, 03:56:36 AM
 #4

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 Wink.
odolvlobo
Legendary
*
Offline Offline

Activity: 4312
Merit: 3214



View Profile
December 26, 2020, 05:31:48 AM
 #5

"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)

Join an anti-signature campaign: Click ignore on the members of signature campaigns.
PGP Fingerprint: 6B6BC26599EC24EF7E29A405EAF050539D0B2925 Signing address: 13GAVJo8YaAuenj6keiEykwxWUZ7jMoSLt
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6735


bitcoincleanup.com / bitmixlist.org


View Profile WWW
December 26, 2020, 07:21:10 AM
 #6

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 Wink.

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. } }

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Skybuck (OP)
Full Member
***
Offline Offline

Activity: 384
Merit: 110


View Profile
December 27, 2020, 08:03:34 AM
 #7

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 Smiley

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 ? Wink Smiley)
odolvlobo
Legendary
*
Offline Offline

Activity: 4312
Merit: 3214



View Profile
December 27, 2020, 08:29:41 AM
 #8

...
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.

Join an anti-signature campaign: Click ignore on the members of signature campaigns.
PGP Fingerprint: 6B6BC26599EC24EF7E29A405EAF050539D0B2925 Signing address: 13GAVJo8YaAuenj6keiEykwxWUZ7jMoSLt
Skybuck (OP)
Full Member
***
Offline Offline

Activity: 384
Merit: 110


View Profile
January 01, 2021, 01:28:50 PM
Last edit: January 01, 2021, 01:42:51 PM by Skybuck
 #9

...
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 ? Wink Smiley
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6735


bitcoincleanup.com / bitmixlist.org


View Profile WWW
January 01, 2021, 03:24:02 PM
 #10

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 ? Wink Smiley

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).

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
odolvlobo
Legendary
*
Offline Offline

Activity: 4312
Merit: 3214



View Profile
January 02, 2021, 01:44:46 AM
 #11

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.


Join an anti-signature campaign: Click ignore on the members of signature campaigns.
PGP Fingerprint: 6B6BC26599EC24EF7E29A405EAF050539D0B2925 Signing address: 13GAVJo8YaAuenj6keiEykwxWUZ7jMoSLt
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!