C++模板,静态函数专用化

C++ template, static function specialization

本文关键字:专用 静态函数 模板 C++      更新时间:2023-10-16

我的模板有语法错误

我想部分专化我的模板类的静态函数

类.hpp

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

类.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

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

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

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

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

如果你像这样专用了类,你只能在类内部声明函数,并在类外部定义它,如下所示:

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

如果您事先没有对其进行专用化,则由于使用不完整的类型而收到编译错误。

您可能还会发现有关在类模板中专门化单个函数的问题和答案相关。

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

您可以部分专攻整个班级。

或者作为替代方法,您可以将实现转发给某个帮助程序:

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

    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);
        }
    

在做了一些研究之后;不允许对类模板的成员函数进行部分专用化,因此必须对整个类进行专用化,如果实际类非常大,这可能是一个问题。如果您尝试将实现与声明分离,则包装器或帮助程序将起作用,但您必须首先定义它和部分专用化。在此处查看此代码,了解它使用 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

我的班级.cpp

#include "MyClass.h"

主.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