c++ - 如何结束模板层?

标签 c++ templates

当有:

template <typename Super>
class Whatever : public Super
{
    ...
};

是否有可能创建 Whatever 类而不派生自某些东西?

这是更轻的版本吗?

struct BlankType{};

Whatever<BlankType> w;

////////////////////////////////////

一些背景:

我将我的代码组合成模板层,如上面的 Whatever。所以我可以这样做:

typedef Whatever<Whenever<Wherever<>>>> MyCombinedType

其实我做不到。我必须做的

typedef Whatever<Whenever<Wherever<BlankType>>>> MyCombinedType

并且类型也变为 BlankType。 我无法将 Wherever 设置为“不可分层”,因为当我只做

typedef Whatever<Whenever<>>> MyCombinedType

问题又会出现...

最佳答案

如果你想创建不是派生自某物的 Whatever 类,你可以简单地定义它的规范如下:

class BlankType {};
template<typename T = BlankType> class Whatever : public T {};
template<> class Whatever<BlankType> {};

关于c++ - 如何结束模板层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6982689/

相关文章:

c++ - 如何创建一个对象创建函数,该函数将由与其关联的名称调用?

c++ - 为 C++ 类中基于模板的对象重载 <<

php - 我如何为 Joomla 1.6 创建自定义错误页面 (404)?

javascript - 在渲染时转义 html 中的 angularJS 术语

c++ - "InitGoogleLogging"是做什么的?

c++ - SFML 不会画任何东西?

c++ - 物体相互碰撞

c++ - 全局访问单例数据

C++ 模板转换运算符 - 请求从 到非标量类型的转换

html - 使用Golang的HTML模板中的发件人姓名邮件