模板类中方法的部分专门化 [英] Partial specialization of a method in a templated class

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

问题描述

给定:

  struct A 
{
virtual bool what()= 0;
};

template< typename T,typename Q>
struct B:public A
{
virtual bool what();
};

我想部分专门化什么

  template< typename T,typename Q> 
bool B< T,Q> :: what()
{
return true;
}

template< typename Q>
bool B< float,Q> :: what()
{
return false;
}

但看起来这是不可能的11?)所以我试过SFINAE:

  template< typename T& 
typename std :: enable_if< std :: is_same< T,float> :: value,bool> :: type B< T> :: what()
{
return true;
}

template< typename T>
typename std :: enable_if<!std :: is_same {
return false;
}

这也不行,我不知道为什么, ?因此,我找到此主题,最后以:

  template< typename T,typename Q> 
struct B:public A
{
virtual bool what()
{
return whatimpl(std :: is_same< T,float>());
}

bool whatimpl(std :: false_type)
{
return false;
}

bool whatimpl(std :: true_type)
{
return true;
}
};

这个最终的解决方案可行,但为什么 enable_if 技术工作?我也非常开放的建议一个更干净的答案,我还没有遇到。



我尽可能简化了我的例子 - 在我的实际使用情况 what()不是调用 what ,实际上做了一个公平的工作,我想'专门化'一个用户定义类型,而不是 float

解决方案

部分专业化只有类模板(见14.5.5类模板部分特化)



对于类模板的成员,只允许显式特化。



14.7(3)说:



可以为函数模板,类模板, b $ b模板或成员模板。 template<> 引入了一个显式专门化声明。



所有以

开头的定义

  template< typename T> 

不是类模板专用化成员的语法。



对于SFINAE尝试,它失败了,因为这里既没有重载也没有特殊化同时定义用于重载分辨率或在选择适当专门化时的一组候选函数)。 what()被声明为类模板的单个方法,并且应该具有单个定义,并且该定义应该具有以下形式:

  template< typename T,typename Q> 
B< T,Q> :: bool what(){...}

或者也可以明确地专门用于B类的特定实例化:

  template< 
B< SomeParticularTypeT,SomeParticularTypeTypeQ> :: bool what(){...}

任何其他形式的句法无效,所以SFINAE不能帮助。


Given:

struct A
{
    virtual bool what() = 0;
};

template<typename T, typename Q>
struct B : public A
{
    virtual bool what();
};

I want to partially specialize what like:

template<typename T, typename Q>
bool B<T, Q>::what()
{
    return true;
}

template<typename Q>
bool B<float, Q>::what()
{
    return false;
}

But it appears that this isn't possible (is it in C++11?) so I tried SFINAE:

template<typename T>
typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what()
{
    return true;
}

template<typename T>
typename std::enable_if<!std::is_same<T, float>::value, bool>::type B<T>::what()
{
    return false;
}

This also doesn't work, I have no idea why though, does anyone? So I found this thread and ended up with:

template<typename T, typename Q>
struct B : public A
{
    virtual bool what()
    {
        return whatimpl(std::is_same<T, float>());
    }

    bool whatimpl(std::false_type)
    {
        return false;
    }

    bool whatimpl(std::true_type)
    {
        return true;
    }
};

This final solution works, but why doesn't the enable_if technique work? I'm also very open to suggestions of a cleaner answer that I haven't encountered yet.

I simplified my examples as much as possible - in my real use case what() isn't called what and actually does a fair bit of work, and I'll want to 'specialize' on a user defined type, not float.

解决方案

Partial specialization is explicitly permitted by the standard only for class templates (see 14.5.5 Class template partial specializations)

For members of class template only explicit specialization is allowed.

14.7 (3) says:

An explicit specialization may be declared for a function template, a class template, a member of a class template or a member template. An explicit specialization declaration is introduced by template<>.

So any definition starting with

template<typename T>  

is not an allowed syntax for member of class template specialization.

[edit]

As to SFINAE attempt, it failed because actually there are neither overloads nor specializations here (SFINAE works while defining a set of candidate functions for overload resolution or while choosing proper specialization). what() is declared as a single method of class template and should have a single definition, and this definition should have a form:

template<typename T, typename Q> 
B<T,Q>:: bool what(){...}

or may be also explicitly specialized for particular instantiation of class B:

template<> 
B<SomeParticularTypeT,SomeParticularTypeTypeQ>:: bool what(){...}

Any other forms are syntacticaly invalid, so SFINAE can't help.

这篇关于模板类中方法的部分专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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