专门在一个大模板类中的单一方法 [英] Specializing single method in a big template class

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

问题描述

在C ++中,如果你想在模板类中部分专门化一个方法,你必须专门化整个类(例如

In C++ if you want to partially specialize a single method in a template class you have to specialize the whole class (as stated for example in Template specialization of a single method from templated class with multiple template parameters)

然而,在具有多个模板参数的更大的模板类中,当每个都影响单个函数时,这变得令人讨厌。使用N参数你需要专门化类2 ^ N次!

This however becomes tiresome in bigger template classes with multiple template parameters, when each of them influences a single function. With N parameters you need to specialize the class 2^N times!

然而,使用C ++ 11我认为可能有一个更优雅的解决方案,但我不是肯定如何接近它。也许以某种方式与 enable_if ?任何想法?

However, with the C++11 I think there might a more elegant solution, but I am not sure how to approach it. Perhaps somehow with enable_if? Any ideas?

推荐答案

除了Torsten提出的基于继承的解决方案,您可以使用 std :: enable_if 和默认功能模板参数启用/禁用该功能的某些特殊化。

In addition to the inheritance-based solution proposed by Torsten, you could use std::enable_if and default function template parameters to enable/disable certain specializations of the function.

例如:

template<typename T>
struct comparer
{
    template<typename U = T ,
    typename std::enable_if<std::is_floating_point<U>::value>::type* = nullptr>
    bool operator()( U lhs , U rhs )
    {
        return /* floating-point precision aware comparison */;
    }

    template<typename U = T ,
    typename std::enable_if<!std::is_floating_point<U>::value>::type* = nullptr>
    bool operator()( U lhs , U rhs )
    {
        return lhs == rhs;
    } 
};

我们利用 SFINAE 根据模板参数禁用/启用函数的不同特殊化。因为SFINAE只能依赖于函数参数而不是类参数,所以我们需要一个函数的可选模板参数,它接受类的参数。

We take advantage of SFINAE to disable/enable the different "specializations" of the function depending on the template parameter. Because SFINAE can only depend on function parameters, not class parameters, we need an optional template parameter for the function, which takes the parameter of the class.

我喜欢这个解决方案基于继承,因为:

I prefer this solution over the inheritance based because:


  • 需要较少的键入。减少打字可能导致更少的错误。

  • 所有专业内容都写在课程之内。这种写特殊化的方式拥有原始类中的所有特殊化,并使专门化看起来像函数重载,而不是棘手的基于模板的代码。

  • It requires less typing. Less typing probably leads to less errors.
  • All specializations are written inside the class. This way to write the specializations holds all of the specializations inside the original class , and make the specializations look like function overloads, instead of tricky template based code.

但是对于没有实现可选功能模板参数的编译器(就像VS2012中的MSVC),此解决方案不工作,基于继承的解决方案。

But with compilers which have not implemented optional function template parameters (Like MSVC in VS2012) this solution does not work, and you should use the inheritance-based solution.

编辑:可以覆盖包含模板函数的非实现默认函数模板参数具有委托工作的其他功能:

You could ride over the non-implemented-default-function-template-parameters wrapping the template function with other function which delegates the work:

template<typename T>
struct foo
{
private:
    template<typename U>
    void f()
    {
        ...
    }

public:
    void g()
    {
        f<T>();
    }
};

当然,编译器可以轻松地内联 g c>抛弃包装调用,因此这个替代方案没有性能损失。

Of course the compiler can easily inline g() throwing away the wrapping call, so there is no performance hit on this alternative.

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

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