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();
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();
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?