具有多个模板参数的C ++单模板专门化 [英] C++ single template specialisation with multiple template parameters
问题描述
Hallo!
我想只专注于两种模板类型之一。例如。 template< typename A,typename B>类X
应该对单个函数有一个特殊的实现 X< float,sometype> :: someFunc()
。
示例代码:
main.h:
#include< iostream>
template< typename F,typename I>
class B
{
public:
void someFunc()
{
std :: cout< 正常< std :: endl;
};
void someFuncNotSpecial()
{
std :: cout< 正常< std :: endl;
};
};
template< typename I>
void B< float,I> :: someFunc();
main.cpp:
#include< iostream>
#includemain.h
using namespace std;
template< typename I>
void B< float,I> :: someFunc()
{
cout< 特殊<< endl;
}
int main(int argc,char * argv [])
{
B< int,int& b1;
b1.someFunc();
b1.someFuncNotSpecial();
B< float,int> b2;
b2.someFunc();
b2.someFuncNotSpecial();
}
B类的编译失败
。是真的,这是不可能在C + +在这种方式?
模板< float ,类型名I>
导致
void B< float,I> :: someFunc();
main.h:26:error:'float'不是模板常量的有效类型参数
模板< typename I>
导致
void B< float,I> :: someFunc();
main.h:27:错误:无效使用不完整的类'B类'
我正在使用gcc。
我不想专门对整个类,因为有其他函数没有专门化。
您必须提供类模板 B
的部分专业化:
template< typename I>
class B< float,I>
{
public:
void someFunc();
};
template< typename I>
void B< float,I> :: someFunc()
{
...
}
您还可以在专业化内定义 someFunc
。
但是,如果你只想专门化一个函数,而不是一个类做。 g。
template< typename F,typename I>
void someFunc(F f,I i){someFuncImpl :: act(f,i); }
template< typename F,typename I>
struct someFuncImpl {static void act(F f,I i){...}};
//部分专业化
模板< typename I>
struct someFuncImpl< float,I> {static void act(float f,I i){...}};
但是你不能在没有这个技巧的情况下专门化一个函数模板。
Hallo!
I would like to specialise only one of two template types. E.g. template <typename A, typename B> class X
should have a special implementation for a single function X<float, sometype>::someFunc()
.
Sample code:
main.h:
#include <iostream>
template <typename F, typename I>
class B
{
public:
void someFunc()
{
std::cout << "normal" << std::endl;
};
void someFuncNotSpecial()
{
std::cout << "normal" << std::endl;
};
};
template <typename I>
void B<float, I>::someFunc();
main.cpp:
#include <iostream>
#include "main.h"
using namespace std;
template <typename I>
void B<float, I>::someFunc()
{
cout << "special" << endl;
}
int main(int argc, char *argv[])
{
B<int, int> b1;
b1.someFunc();
b1.someFuncNotSpecial();
B<float, int> b2;
b2.someFunc();
b2.someFuncNotSpecial();
}
Compilation fails for class B
. Is it true, that this is not possible in C++ in this way? What would be the best workaround?
[edit]
template <float, typename I>
void B<float, I>::someFunc();
leads to
main.h:26: error: ‘float’ is not a valid type for a template constant parameter
template <typename I>
void B<float, I>::someFunc();
leads to
main.h:27: error: invalid use of incomplete type ‘class B’
And I'm using gcc.
[edit]
I don't want to specialise the whole class, as there are other functions that don't have a specialisation.
You have to provide a partial specialization of the class template B
:
template <typename I>
class B<float, I>
{
public:
void someFunc();
};
template <typename I>
void B<float, I>::someFunc()
{
...
}
You can also just define someFunc
inside the specialization.
However, if you only want to specialize a function, and not a class do e. g.
template <typename F, typename I>
void someFunc(F f, I i) { someFuncImpl::act(f, i); }
template <typename F, typename I>
struct someFuncImpl { static void act(F f, I i) { ... } };
// Partial specialization
template <typename I>
struct someFuncImpl<float, I> { static void act(float f, I i) { ... } };
But you can't specialize a function template without this trick.
这篇关于具有多个模板参数的C ++单模板专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!