【问题标题】:C++ - Template Specialization & Partial SpecializationC++ - 模板特化和部分特化
【发布时间】:2012-11-06 19:59:04
【问题描述】:

我一直在互联网和 stackoverflow 上寻找具体答案,但我似乎找不到。我必须创建一个通用类,然后实现特定的功能。我的具体说明是:您需要使用模板表达式参数和模板类专业化和部分专业化。

我有一个模板类:

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()

2) 对于 x = 2 和 y= 2 的字符 Z,J 会做一些事情;对于其他所有事情,它会做其他事情

3) 只有在 T 为 char 的 Z 中,数组才会被初始化为某个值。其他的都是 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 存在)这不起作用:

template <class T>
void Z<T,2,2>::J() { //something }

我的问题是:

有没有简单的方法来实现上述项目?我需要将所有其他方法保留在 Z 中。给出提示或指出正确的方向(可能我错过了一个问题,因为有很多问题)会有所帮助。

谢谢。

【问题讨论】:

  • 你有哪些错误? J 是什么?不清楚你想做什么。
  • J 是一些函数。我需要做的是列出1,2,3。我的错误是(下一个 cmets):
  • For Z::Z() { //stuff } : “'之前
  • 并假设 J 存在:void Z::J() {//stuff} 给出错误:“无效使用未定义类型class Z&lt;char, x, y&gt;'" and "template definition of non-template void Z::J()' "

标签: c++ templates class-template


【解决方案1】:

看来你只想定义一些特化的部分函数:如果print()char特化和一般情况之间没有变化,你似乎不想重新定义它。

// 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() { /* ... */ }

但是,它不是这样工作的。 类的部分或全部特化几乎没有共同点,除了模板参数的“原型”

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

您必须声明和定义每个(部分)特化:

// 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的继承:

// 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++ 标准,请参阅 n3322n3329)。

【讨论】:

  • 是的,我知道这一点。但我想知道是否有一种更简单的方法可以做到这一点,而无需到处重复代码。 (因为显然这是一件非常糟糕的事情)
  • 这不是继承,因为您仍然必须在解决方案中的任何地方复制代码(与以前相同)。无论如何,我会尝试让一些继承工作。
【解决方案2】:

你可以看看这个课程http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-C-5-of-n

虽然您无法为函数模板定义部分特化,但您可以为类或结构模板定义部分特化。

template<typename T> struct helper {
    static void doThingy(){}
};

template<typename X> struct helper<X*> {
    static void doThingy(){}
};

Helper(double*)::doThingy();

在这个例子中,只有当模板中的类型是指针类型时,您才希望在 doThingy() 中专门化行为。在这种情况下,您不能使用方法 doThingy() 的重载。这是因为您不能重载没有参数的函数。但是您可以对 struct helper 进行部分专业化。在专门的模板中,您为 doThingy() 实现了希望的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多