Default Arguments in a Template

Is it possible to have something like this:

template<class T = unsigned = 64>
class X
{
};

It basically says that if nothing is specified, then unsigned should be a type, and its value should be 64.

+3
source share
2 answers

Taken literally, it does not make sense. Explain what you are trying to do first.

Templates in C ++ have three types of parameters: 1) type parameters, 2) values ​​(atypical) parameters, 3) template parameters.

class T, ( ). "" . unsigned, . "" T unsigned. unsigned T class X. T , 64.

, , - kind 1 kind 2. -, . -, , . .

, : ,

template <unsigned N = 64>
class X {
};

, , : . , , . - X, , .

+4

. - :


template < typename T = unsigned, T value = 64>
struct X{};

: , , - , :


template < unsigned N = 64 >
struct unsigned_
{
  typedef unsigned type;
  enum { value = N };
};

// alternative versions...int, long, etc...

template < typename T = unsigned_<64> >
struct X {};

. , boost:: enable_if is_fundamental, , T:: type T:: value.

, , ... , , .

+3

Source: https://habr.com/ru/post/1745159/


All Articles