C++ 模板,静态函数特化

C++ template, static function specialization

我的模板存在语法错误

我想部分特化我模板的静态函数class

class.hpp

template <typename Foo, size_t bar = 26>
class MyClass
{
    MyClass();
    static void function();
};

#include "class.tpp"

class.tpp

template <typename Foo, bar>
MyClass<Foo, bar>::MyClass()
{ }

template <typename Foo>
inline
void
MyClass<Foo, 6>::function()
{
    // ...
}

template <typename Foo>
inline
void
MyClass<Foo, 26>::function()
{
    // ...
}

error: template definition of non-template

我只想为 bar == 26 和 bar == 6 实现 MyClass<Foo, bar>::function

如何正确地做到这一点? 谢谢

该函数本身不是模板,它仅在 class 模板中。您可以针对这些情况专门化 class,但不能专门化函数本身。

template <class Foo>
class MyClass<Foo, 26>
{
    static void function() { ... }
};

如果你像这样特化了class,你只能在class里面声明函数,然后像这样在外面定义它:

template <class Foo>
void MyClass<Foo, 26>::function() { ... }

如果你没有预先特化它,你会因为使用不完整的类型而出现编译错误。

您可能还会发现 this 有关在 class 模板中专门化单个函数的问答。

你不能像那样部分专门化方法。

您可以部分特化整个 class。

或者作为替代方案,您可以将实施转发给一些帮助者:

  • 结构,您可以根据需要进行专业化。
  • 过载(使用一些调度):

    namespace detail
    {
        template <typename Foo, std::size_t bar>
        void function_impl(MyClass<Foo, bar>& that)
        {
            // Generic case.
        }
    
        template <typename Foo>
        void function_impl(MyClass<Foo, 6>& that)
        {
            // special case <Foo, 6>.
        }
    
        template <typename Foo>
        void function_impl(MyClass<Foo, 26>& that)
        {
            // special case <Foo, 26>.
        }
    }
        template <typename Foo, std::size_t bar>
        inline
        void
        MyClass<Foo, bar>::function()
        {
            detail::function_impl(*this);
        }
    

经过一些研究;不允许对 class 模板的成员函数进行部分特化,因此必须对整个 class 进行特化,如果实际的 class 非常大,这可能会成为一个问题。如果您试图将实现与具有包装器或帮助器的声明分开,但是您必须首先定义它和部分专业化。在此处查看此代码,因为它使用 MSVC 2015 CE 编译、构建和输出适当的值。

MyClass.h

#ifndef MY_CLASS_H
#define MY_CLASS_H

#include <iostream>

// Helper - Wrapper Class
template<typename Foo, size_t Bar>
class specialized {
public:
    inline static void function();
};

// Partial Specialization
template<typename Foo>
class specialization<Foo, 6> {
public:
    inline static void function();
};

// Actual Class
template<typename Foo, size_t Bar = 26>
class MyClass {
private:
    specialized<Foo, Bar> func;
public:
    MyClass();

    inline void function(); // Works
    // inline static void function(); // Compiler Error
}; // MyClass

#include "MyClass.inl"

#endif // MY_CLASS_H

MyClass.inl

// Helper - Wrapper
template<typename Foo, size_t Bar>
inline void specialized<Foo, Bar>::function() {
    std::cout << "26" << std::endl;
} // function

// Specialized
template<typename Foo>
inline void specialized<Foo, 6>::function() {
    std::cout << "6" << std::endl;
} // function

// Constructor
template<typename Foo, size_t Bar>
MyClass<Foo, Bar>::MyClass() {
} // MyClass

// Class Member Function
template<typename Foo, size_t Bar>
inline void MyClass<Foo, Bar>::function() {
    func.function();
} // function

MyClass.cpp

#include "MyClass.h"

Main.cpp

#include "MyClass.h"

int main() {
    MyClass<float, 6> a;
    a.function(); // Prints Out 6

    MyClass<float, 26> b;
    b.function(); // Prints Out 26

    MyClass<float> c;
    c.function(); // Prints Out 26

    MyClass<float, x != 6> d;
    d.function(); // Prints Out 26

    return 0;
} // Main