使用模板参数为零的模板可以做什么? [英] What can you do with templates with zero template parameters?

查看:80
本文介绍了使用模板参数为零的模板可以做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前面我学到了,你可以创建零参数的模板。虽然无法直接创建它们,但您可以使用成员模板

I learned some time ago that you can create templates with zero parameters. While it is not possible to create them directly, you can use member templates

template<typename ...T> 
struct Maker {
   template<T...>
   struct HasNParams { };
};

Maker<>::HasNParams<> hnp;

我不知道这是不是要有良好的形状,你能用这些野兽做什么。你可以将它们作为模板参数传递,并创建明确的特殊化(我猜测,唯一的情况是空的情况)。

I wonder whether this is intended to be well-formed and what you can do with these beasts. Can you pass them as template arguments, and create explicit specializations (I guess the only scenario is for the empty case then)?

推荐答案

冒着明显风险,结束递归实例化。

At the risk of sounding obvious, ending recursive instantiation.

template<typename Arg, typename ...T> 
struct Maker : public Maker<T...>
{
   template<T...>
   struct HasNmin1Params { };
};

这里的要点是 Maker的实际参数列表不为空,但我们只在 HasNminOneParams 中使用N-1个参数。

The point here is that the actual argument list to Maker isn't empty, but we only use N-1 arguments in HasNminOneParams.

这篇关于使用模板参数为零的模板可以做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆