Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this template parameter mean?

Tags:

c++

templates

For the following code, what does std::uint64_t = 0 mean?

template< class T, std::uint64_t = 0 >
struct Dummy {  
   T value;
};
like image 349
veda Avatar asked Jun 08 '26 04:06

veda


1 Answers

It's a non-type template parameter of type std::uint64_t with a default value of 0.

Note that the parameter is unnamed, so you can't use it directly in Dummy.

However, there are still several uses for this template parameter, e.g. you can use a different value for this parameter to select specializations of Dummy:

// specialization
template< class T>
struct Dummy<T, 42> {
   // ... 
};

Now Dummy<int> or Dummy<int, 0> will use the primary template, but Dummy<int, 42> will use the partial specialization. One of the common uses of this is in a technique called SFINAE.

like image 83
cigien Avatar answered Jun 10 '26 17:06

cigien