C++模板,静态函数专用化
C++ template, static function specialization
我的模板有语法错误
我想部分专化我的模板类的静态函数
类.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
相关文章:
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 检查编译时是否存在静态函数
- 名称隐藏对静态函数继承的实例使用
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 类中静态函数C++意外结果
- 在工人类中使用不同类的静态函数进行实验
- 类 Referention 中C++回调函数引用非静态函数
- 指向模板上下文中的成员函数或静态函数的指针
- 如何检测 Clang AST C++中的静态函数
- 内联asm编译器屏障(内存阻塞器)是算作外部函数,还是算作静态函数调用
- 如何在静态函数中使用成员函数数组
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 生成代码(在编译时)以调用模板的每个实例化的静态函数
- C++无法访问或使用静态函数
- 如何将 cpp 文件中的静态函数公开给其他文件
- 模板专用化与静态函数模板
- 使用g++编译时静态函数的模板专用化
- C++模板,静态函数专用化
- 继承的静态函数能否访问重写的静态专用数据成员
- 专用嵌套类的静态函数会导致编译器错误 C3855 (MSVC9)