Samuel Danielson
Samuel Danielson

Reputation: 5471

Can a C++ template parameter be empty?

I'm trying to do something like this.

template <typename T>
struct thingo {
  int always;
  T sometimes;
};

thingo <> compile_error; // <- wont compile

thingo <nullptr_t> wastes_space; // compiles but nullptr_t takes space anyway

Is inheriting from an int wrapper the only way to accomplish this?

Upvotes: 0

Views: 1852

Answers (1)

user2249683
user2249683

Reputation:

What about:

struct None {};

// Or without an extra struct:
// typedef void None;

template <typename T = None>
struct thingo {
  int always;
  T sometimes;
};

template <>
struct thingo<None> {
  int always;
};

Upvotes: 7

Related Questions