Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • SymEngine source code: https://github.com/symengine/symengine
  • Piranha source code: https://github.com/bluescarni/piranha

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!

like image 380
Grayscale Avatar asked Jul 26 '17 19:07

Grayscale


People also ask

What are curly braces in C?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true.

Why initialize with curly braces C++?

Using curly braces to initialize a variable also prevents narrowing. Narrowing, or more precisely narrowing conversion, is the implicit conversion of arithmetic values that includes​ a loss of accuracy.

What is the purpose of {} squiggly braces in Java?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What is double curly braces in JSON?

Data is represented in name/value pairs. Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma). Square brackets hold arrays and values are separated by ,(comma).

What are curly braces in C++?

Curly braces (also referred to as just “braces” or as “curly brackets”) are a major part of the C and C++ programming languages. They are used in several different constructs, outlined below, and this can sometimes be confusing for beginners. An opening curly brace, {must always be followed by a closing curly brace }.

Why do we see two consecutive curly braces in Java?

If you see two consecutive curly braces { in java code, it is an usage of double brace initialization. First brace is creation of an anonymous inner class. Without considering the second brace if you see the first brace alone then its nothing new for us. We have created many anonymous inner classes in such a way.

What is double brace initialization in Java?

Java Double Brace Initialization. Double brace initialization is a combination of two separate process in java. There are two { braces involved in it. If you see two consecutive curly braces { in java code, it is an usage of double brace initialization.

When to use brace initialization in C++?

You can use brace initialization anywhere you would typically do initialization—for example, as a function parameter or a return value, or with the new keyword: In /std:c++17 mode, the rules for empty brace initialization are slightly more restrictive. See Derived constructors and extended aggregate initialization.


1 Answers

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
like image 119
Tobias Ribizel Avatar answered Sep 24 '22 15:09

Tobias Ribizel