未命名的非类型模板参数有什么意义? [英] What's the point of unnamed non-type template parameters?

查看:85
本文介绍了未命名的非类型模板参数有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据引用,即非类型模板参数的名称即使分配默认值也是可选的(请参阅(1)和(2)).因此,这些模板结构是有效的:

According to the reference, the name of a non-type template parameter is optional, even when assigning a default value (see (1) and (2)). Therefore these template structs are valid:

template <int> struct Foo {};
template <unsigned long = 42> struct Bar {};

我还没有看到访问非类型参数值的可能性. 我的问题是:未命名/匿名非类型模板参数有什么意义?为什么名称是可选的?

I haven't seen a possibility of accessing the values of the non-type parameters. My question is: What's the point of unnamed/anonymous non-type template parameters? Why are the names optional?

推荐答案

首先,我们可以从定义中拆分声明. 因此,声明中的名称并没有真正的帮助.和名称可能会在定义中使用

First, we can split declaration from definition. So name in declaration is not really helpful. and name might be used in definition

template <int> struct Foo;
template <unsigned long = 42> struct Bar;

template <int N> struct Foo {/*..*/};
template <unsigned long N> struct Bar {/*..*/};

专业化是定义的特例.

然后可以不使用名称,因此我们可以忽略它:

Then name can be unused, so we might omit it:

template <std::size_t, typename T>
using always_t = T;

template <std::size_t ... Is, typename T>
struct MyArray<std::index_sequence<Is...>, T>
{
    MyArray(always_t<Is, const T&>... v) : /*..*/
};

或用于SFINAE

template <typename T, std::size_t = T::size()>
struct some_sized_type;

这篇关于未命名的非类型模板参数有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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