C ++模板参数可以为空吗?

时间:2014-11-17 19:06:57

标签: c++

我试图做这样的事情。

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

从int包装器继承是实现此目的的唯一方法吗?

1 个答案:

答案 0 :(得分:6)

怎么样:

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;
};