-1

I would like to know how this template works. Why did we use T{} to initialize the template parameter?

template<typename T, T Val = T{}>
T bar();
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
  • 1
    there is nothing that could "work", its just a declaration. What exactly dont you understand? Your second question nobody but you can answer, who is "we" anyhow? – 463035818_is_not_an_ai Feb 04 '19 at 21:47
  • Read up on [value initialization](https://en.cppreference.com/w/cpp/language/value_initialization). – R Sahu Feb 04 '19 at 21:50

1 Answers1

3

The curly braces are list initialization, introduced in C++11.

It value-initializes the template parameter Val, which is of templated type T.

You could have just as well done:

template<typename T, T Val = T()>
T bar();

For the advantages and disadvantages, see: Why is list initialization (using curly braces) better than the alternatives?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • To add to the answer: The curly braces existed prior to C++11 too. Back then it was called aggregate initialization (which is now a form of list initialization), and was for aggregate types only. – eerorika Feb 04 '19 at 22:35