Default template parameter with a default value?

Viewed 101

We can have the following and it is valid:

template<std::size_t Size = 10>
fn(){...}


template<typename Size = std::size_t>
fn() {...}

But how to do this?

template<typename Size = std::size_t=10>
fn() {...}
2 Answers

A template parameter is either a type or a value, it isn't both.

You can do it with two parameters

template<typename Size = std::size_t, Size s = 10>

You could require that your parameter be a specialization of std::integral_constant (or some other type that provides both 'type' and 'value' members:

template<typename Arg = std::integral_constant<size_t, 10>>
struct foo
{ };

but in general I would say the two parameter method is preferable.

Related