使用默认参数专门化内部模板 [英] Specializing inner template with default parameters

查看:128
本文介绍了使用默认参数专门化内部模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当参数全部已知时,我无法专门处理内部模板。这里有一个例子:

  template<类型名称T0> 
struct outer
{
template< typename T1 = void,typename T2 = void>
struct inner
{
typedef T1 type;
};
};
template<类型名称T0>
template<类型名T1>
struct outer< T0> :: inner< double,T1> {typedef int type; };

这很好。如果我这样指定内部模板,那么它不会:

  template<类型名称T0> 
template< >
struct outer< T0> :: inner< double,void> {typedef int type; };

为此,我得到错误消息类模板没有明确专用...在部分特化中不使用的模板参数:... T0。

我也尝试过:

  template<类型名称T0> 
struct outer< T0> :: inner< double,void> {typedef int type; };

我预计这会失败,错误信息并不奇怪。它是:太少的模板参数列表。



那么,这是正确的方法是什么?

解决方案

这不是允许。



根据C ++ 11标准的第14.7.16节:


在类模板或在命名空间范围中出现
的成员模板的成员的显式专门化声明中,成员模板和一些它的封闭类模板可以保持
非特殊化,除了声明不明确专门化类成员模板,如果它的封闭
类模板没有明确专用
。 [...]


此外,C ++ 11标准的第14.7.3 / 15段说: b
$ b


成员或成员模板可以嵌套在许多封闭类模板中。在对这样的成员明确的
专用化中,成员声明之前应当是每个
包含类模板的模板<> 专门。 [示例

 模板< class T1>类A {
template< class T2> class B {
void mf();
};
};
模板<>模板<> class A< int> :: B< double> ;;
模板<>模板<> void A< char> :: B< char> :: mf();

- end example ]


< blockquote>

I'm having trouble specializing an inner template when it's parameters are all known. Here's an example:

template < typename T0 >
struct outer
{
    template < typename T1 = void, typename T2 = void >
    struct inner
    {
        typedef T1 type;
    };
};
template < typename T0 >
template < typename T1 >
struct outer<T0>::inner<double,T1> { typedef int type; };

This works just fine. If I instead specify the inner template like so, it does not:

template < typename T0 >
template < >
struct outer<T0>::inner<double,void> { typedef int type; };

For this I get the error message, "invalid explicit specialization before ‘>’ token...enclosing class templates are not explicitly specialized...template parameters not used in partial specialization:...T0". Not sure WTAF is going on here.

I also tried this:

template < typename T0 >
struct outer<T0>::inner<double,void> { typedef int type; };

I expected this to fail and the error message is not surprising. It was: "too few template-parameter-lists".

So, what's the correct way to do this? I can of course hack around it, but if I don't have to I'd prefer not.

解决方案

That is not allowed. You cannot fully specialize a member of a class template that has not been itself fully specialized.

Per paragraph 14.7.16 of the C++11 Standard:

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. [...]

Also, paragraph 14.7.3/15 of the C++11 Standard says:

A member or a member template may be nested within many enclosing class templates. In an explicit specialization for such a member, the member declaration shall be preceded by a template<> for each enclosing class template that is explicitly specialized. [ Example:

template<class T1> class A {
     template<class T2> class B {
         void mf();
     };
};
template<> template<> class A<int>::B<double>;
template<> template<> void A<char>::B<char>::mf();

end example ]

这篇关于使用默认参数专门化内部模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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