简体   繁体   English

模板类,功能专业化

[英]Template class, function specialization

I want to have a template class that looks something like what I have down below. 我想要一个看起来像我下面的模板类。 Then, I want a function in it with a template specialization depending on a CLASS template parameter. 然后,我想要一个带有模板特化的函数,具体取决于CLASS模板参数。 How do I make this work? 我该如何工作? I realize the code I provided is wrong on many levels, but it's just to illustrate the concept. 我意识到我提供的代码在很多层面都是错误的,但它只是为了说明这个概念。

template <typename _T, size_t num>
class Foo
{
    // If num == 1, I want to call this function...
    void Func<_T, 1>()
    {
        printf("Hi!");
    }

    // Otherwise, I want to call this version.
    void Func<_T, num>()
    {
        printf("Hello world!");
    }
};
struct Otherwise { };
template<size_t> struct C : Otherwise { };

// don't use _Uppercase - those names are reserved for the implementation
// (i removed the '_' char)
template <typename T, size_t num>
class Foo
{
public:
    void Func() { Func(C<num>()); }

private:
    // If num == 1, I want to call this function...
    void Func(C<1>)
    {
        printf("Hi 1!");
    }

    // If num == 2, I want to call this function...
    void Func(C<2>)
    {
        printf("Hi 2!");
    }

    // Otherwise, I want to call this version.
    void Func(Otherwise)
    {
        printf("Hello world!");
    }

    //// alternative otherwise solution:
    // template<size_t I>
    // void Func(C<I>) { .. }
};

There are no partial specialisations of function templates, and to partially specialise a member you need to first partially specialise the class template. 函数模板没有部分特化,并且要部分地特化成员,首先需要部分专门化类模板。

template< typename _T, size_t num >
struct Foo {
    void Func() {
        printf("Hello world!");
    }
};

template< typename _T >
struct Foo< _T, 1 > {
    void Func() {
        printf("Hi!");
    }
};

Now, if Foo also contains methods other than Func whose implementation is independent of the value for num , and you don't want to duplicate their implementation in the specialization of Foo , you can apply the following pattern: 现在,如果Foo还包含Func以外的方法,其实现独立于num的值,并且您不想在Foo复制它们的实现,则可以应用以下模式:

template< typename _T, size_t num >
struct FooFuncBase {
    void Func() {
        printf("Hello world!");
    }
};

template< typename _T >
struct FooFuncBase< _T, 1 > {
    void Func() {
        printf("Hi!");
    }
};

template< typename _T, size_t num >
struct Foo : public FooFuncBase< _T, num > {
  void OtherFuncWhoseImplementationDoesNotDependOnNum() {
    ...
  }
};

Or, using CRTP : 或者,使用CRTP

template< typename _Derived, typename _T, size_t num >
struct FooFuncBase {
    void Func() {
        static_cast< _Derived* >(this)->OtherFuncWhoseImplementationDoesNotDependOnNum();
        printf("Hello world!");
    }
};

template< typename _Derived, typename _T >
struct FooFuncBase< _Derived, _T, 1 > {
    void Func() {
        static_cast< _Derived* >(this)->OtherFuncWhoseImplementationDoesNotDependOnNum();
        printf("Hi!");
    }
};

template< typename _T, size_t num >
struct Foo : public FooFuncBase< Foo< _T, num >, _T, num > {
  void OtherFuncWhoseImplementationDoesNotDependOnNum() {
    printf("Other");
  }
};

This is called partial template specialisation. 这称为部分模板专业化。 It looks like this: 它看起来像这样:

template<typename _T, size_t num>
class FooBase
{
};

template <typename _T, size_t num>
class Foo : public FooBase<_T,num>
{
    void Func()
    {
        printf("Hello world!");
    }
};


template <typename _T>
class Foo<_T,1> : public FooBase<_T,num>
{
    void Func()
    {
        printf("Hi!");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM