What's the point of unnamed non-type template parameters?

蓝咒 提交于 2020-02-02 10:06:26

问题


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?


回答1:


What's the point of unnamed/anonymous non-type template parameters?

I can think of specialization:

template<int = 42>
struct Foo{
   char x;
};

template<>
struct Foo<0> {
   int x;
};

template<>
struct Foo<1> {
   long x;
};

Then:

Foo<0> a; // x data member is int
Foo<1> b; // x data member is long
Foo<7> c; // x data member is char
Foo<>  d; // x data member is char



回答2:


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 {/*..*/};

Specialization is a special case of definition.

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) : /*..*/
};

or used for SFINAE

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



回答3:


Oh, you can access them!

template <int> struct Foo {};

template <int N>
int get(Foo<N>) {
    return N;
}

int main() {
    Foo<3> foo;
    return get(foo);
}

This might be a bit contrived. But in general for some templates you don't want to name them and then it is convenient that you don't have to.



来源:https://stackoverflow.com/questions/59824884/whats-the-point-of-unnamed-non-type-template-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!