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

Can a C++ template parameter be empty?

本文关键字:参数 C++      更新时间:2023-10-16

我正在尝试做这样的事情。

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 包装器继承是实现此目的的唯一方法吗?

怎么样:

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