Grayscale
Grayscale

Reputation: 1532

Double curly braces in C++ constructor

What do the double curly braces in the following line of C++ code mean?

piranha::symbol_set ss({{piranha::symbol(detail::poly_print(var))}});

For context, this is from a file in the SymEngine code ("symengine/polys/uintpoly_piranha.h"), which can be found at the link below, as can the Piranha library which is used in the above line.

I know single curly braces are used as initializer lists, but the meaning of the double curly braces within the set of parentheses makes little sense to me.

The main thing I found on double curly braces was this post, but it does not seem applicable here.

Also, I apologize for linking source code like this but I am unsure of how to make a smaller example given my lack of understanding.

Thanks!

Upvotes: 9

Views: 3763

Answers (1)

Tobias Ribizel
Tobias Ribizel

Reputation: 5421

Curly braces can be used to describe

  • an initializer list, which explains the outer braces (creating an std::initializer_list of symbols, see the corresponding constructor)
  • a shorthand notation to a constructor call, which explains the inner braces (creating an instance of symbol using the move constructor, see the corresponding constructor)
    If the type of a parameter is known beforehand, instead of symbol{parameters} you can just write {parameters}. This also works for return values and variable initialization.

So what actually happens in this line is:

  • a std::string is returned from detail::poly_print(var)
  • This string is used to construct a piranha::symbol
  • This temporary value is then passed to the move constructor (I'm guessing here) of symbol, constructing another symbol
    This seems a bit redundant, but I haven't tried if the code works with only one pair of braces
  • This piranha::symbol is then stored in a std::initializer_list
  • which is then passed to the constructor of piranha::symbol_set

Upvotes: 7

Related Questions