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

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

最佳答案

关于:

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

关于c++ - C++ 模板参数可以为空吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26980016/

相关文章:

c++ - O3是固定的优化序列吗?如何更改 LLVM IR 中的帧指针值?

c++ - glsl 错误 : "v_color" not declared as an output from the previous stage while trying to render wtih tesselation shaders

c++ - 如何有效地从数组中提取唯一值?

c++ - recv() 字符数组大小

c++ - opencv 简单的 Blob 检测,得到一些未检测到的 Blob

c++ - 需要帮助 do-while 循环程序吗? (C++)

C++ 纯虚 const 成员函数

c++ - 指针与 const 和重载的交互

c++ - 如何在 C++ 中使用构造函数初始化二维 vector ?

c++ - 构建共享 ParMETIS-4.0.3