(部分)专门化依赖类型的非类型模板参数 [英] (Partially) specializing a non-type template parameter of dependent type

查看:108
本文介绍了(部分)专门化依赖类型的非类型模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我累了,但是我坚持这个简单的部分专业化,这不工作,因为非类型模板参数专门化模板参数依赖类型'T'

Maybe I'm tired, but I'm stuck with this simple partial specialization, which doesn't work because non-type template argument specializes a template parameter with dependent type 'T':

template <typename T, T N> struct X;
template <typename T>      struct X <T, 0>;

0 > T(0), T {0} (T)0 t帮助。

Replacing 0 by T(0), T{0} or (T)0 doesn't help. So is this specialization even possible?

推荐答案

请参阅[temp.class.spec] 14.5.5 / 8的标准:

See paragraph [temp.class.spec] 14.5.5/8 of the standard:


与专门的
非类型参数相对应的模板参数的类型不取决于
专业化。 [示例:

template <class T, T t> struct C {};
template <class T> struct C<T, 1>; // error

template< int X, int (*array_ptr)[X] > class A {};
int array[5];
template< int X > class A<X,&array> { }; // error

-end example ]

编辑的答案:最简单的解决方法是使用类型1替换非类型模板参数:

The answer to your edit: the easiest workaround is to replace a non-type template parameter with a type one:

#include <type_traits>

template <typename T, typename U>
struct X_;

template <typename T, T N>
struct X_<T, std::integral_constant<T, N>> {};

template <typename T>
struct X_<T, std::integral_constant<T, 0>> {};

template <typename T, T N>
struct X : X_<T, std::integral_constant<T, N>> {};

这篇关于(部分)专门化依赖类型的非类型模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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