C++ - 模板专业化 &部分专业化 [英] C++ - Template Specialization & Partial Specialization
问题描述
我一直在 Internet 和 stackoverflow 上寻找具体的答案,但似乎找不到.我必须创建一个通用类,然后实现特定的功能.我的具体说明是:你需要利用模板表达式参数和模板类特化和部分特化.
I have been looking all over the internet and stackoverflow for a concrete answer but I can't seem to find one. I have to create a generic class and then implement specific functions. My specific instructions were: You need to make use of Template Expression Parameters and Template Class Specialization and Partial Specialization.
我有一个模板类:
template <class T, int x, int y>
class Z {
T **array[x][y];
public:
Z();
void print();
//and other methods
};
我需要:
1) 只有 x= 2 和 y = 2 的 Z 需要有一个公共方法 void J()
1) Only Z's where x= 2 and y = 2 need to have a public method void J()
2) 对于 x = 2 和 y= 2 的字符 Z,J 会做一些事情;对于其他一切,它会做其他事情
2) For char Z's of x = 2 and y= 2 J will do something; for everything else it does something else
3) 仅对于 T 为 char 的 Z 将数组初始化为某个值.对于其他一切,它都是 0
3) For only Z's where T is char will the array be initialized to some value. For everything else it's 0
当然,这是有效的:
template<class T, int x, int y>
Z<T,x,y>::Z<T,x,y>() { //initialize to 0 }
但这不会:
template<int x, int y>
Z<char,x,y>::Z<char,x,y>() { //initialize to something}
同样(假设 J 存在)这不起作用:
And likewise (assume J exists) this does not work:
template <class T>
void Z<T,2,2>::J() { //something }
我的问题是:
有没有什么简单的方法可以实现上述项目?我需要将所有其他方法保留在 Z 中.给出提示或指出正确的方向(可能我错过了一个问题,因为问题太多)会很有帮助.
Is there any simple method for implementing the above items? I need to keep all the other methods in Z. Giving a hint or pointing in the right direction (maybe I missed a question since there are a lot) would be helpful.
谢谢.
推荐答案
看来你只想定义一些专业化的一些函数:如果 print()
在 char 之间没有变化
特化和一般情况,看来你不想重新定义它了.
It seems you want to define only some functions of some specializations : if print()
does not change between the char
specialization and the general case, it seems that you don't want to redefine it.
// What you want to do (illegal in C++)
template<int,typename T>
struct Z
{
T myValue;
Z();
void print() { /* ... */ }
};
template<int i, typename T>
Z<i,T>::Z() { /* ... */ }
template<int i>
Z<i,char>::Z() { /* ... */ }
然而,它不是这样工作的.类的部分或完全特化几乎没有任何共同点,除了模板参数的原型":
However, it does not work like this. Partial or full specializations of class have almost nothing in common, except 'prototype' of template parameters:
// The two following types have only two things related: the template parameter is an int,
// and the second type is a full specialization of the first. There are no relations between
// the content of these 2 types.
template<int> struct A {};
template<> struct A<42> { void work(); };
您必须声明和定义每个(部分)专业化:
You have to declare and define each (partial) specialization:
// Fixed example
template<int,typename T>
struct Z
{
T myValue;
Z();
void print() { /* ... */ }
};
template<int i, typename T>
Z<i,T>::Z() { /* ... */ }
// Specialization for <all-ints,char>
template<int i>
struct Z<i,char>
{
char myValue;
char othervalue;
Z();
void print() { /* Same code than for the general case */ }
};
template<int i>
Z<i,char>::Z() { /* ... */ }
避免重复代码的唯一方法是使用traits的继承:
The only way to escape the code duplication is by using inheritance of traits:
// Example with the print function
template<typename T>
struct print_helper
{
void print() { /* ... */ }
};
// Fixed example
template<int,typename T>
struct Z : public print_helper<T>
{
T myValue;
Z();
};
template<int i, typename T>
Z<i,T>::Z() { /* ... */ }
// Specialization for <all-ints,char>
template<int i>
struct Z<i,char> : public print_helper<char>
{
char myValue;
char othervalue;
Z();
};
template<int i>
Z<i,char>::Z() { /* ... */ }
目前没有重复你不能做你想做的事(删除代码重复的特性是static if
,并且已经被提议用于下一个 C++ 标准,见 n3322 和 n3329).
You cannot do what you want without duplication for the moment (the feature removing code duplication is static if
and has been proposed for the next C++ standard, see n3322 and n3329).
这篇关于C++ - 模板专业化 &部分专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!