简体   繁体   English

模板类问题的部分专业化!

[英]partial specialization of template class issue!

The following code is confusing me 以下代码使我感到困惑

//partial specialization of vector 
template<class t>
class vector
{....
};
template<class t>
//teacher said that this partial specialization will handle all type of pointers
class vector<t*>
...
};

This is confusing me a lot, Suppose t is a char* , as the compiler will first look for the complete specialization as there is no complete specialization here , the partial specialization would be considered. 这让我很困惑,假设t是char *,因为编译器将首先寻找完全专业化,因为这里没有完全专业化,因此将考虑部分专业化。 Now it will become char** in partial specialization as t is char*, then it means we are not achieving the functionality that we want. 现在,它将成为部分专业化的char **,因为t是char *,这意味着我们未实现所需的功能。 Please Explain in more understandable manner. 请以更易理解的方式解释。

The compiler will always looks for the match that is most specialized (and will fail if there's ambiguity). 编译器将始终寻找最专业匹配项(如果存在歧义,将失败)。

Since char** matches both patterns: 由于char**匹配两种模式:

char** = T*  (with T = char*)
char** = T   (with T = char**)

and the former is more specialized, that will be chosen. 而前者则更专业,因此会选择。 (And since char** is really a pointer to something, I think "we are not achieving the functionality that we want" is false.) (并且由于char**实际上是指向某个东西的指针,所以我认为“我们没有实现所需的功能”是错误的。)


Edit. 编辑。

Once the specialization is chosen, all other candidates will be thrown out (recursive substitution will not happen in the matching phase). 一旦选择了专业化,所有其他候选人将被淘汰(在匹配阶段将不会进行递归替换)。 For instance, 例如,

template<class T> struct A {
  typedef T type;
  static const int value = 0;
};
template<class T> struct A<T*> {
  typedef T type;
  static const int value = 12;
};
template<class T> struct A<T********> {
  typedef T type;
  static const int value = 1024;
};
...
A<char**>::type x;                          // <-- Line X
std::cout << A<char**>::value << std::endl; // <-- Line Y

At Line X and Y, the template A is to be instantiated. 在X行和Y行,将实例化模板A You give the first template parameter as char** . 您将第一个模板参数指定为char** A expects either a pattern of T******** or T* or T . A期望使用T********T*T The 2nd one is the best match, so template<class T> struct A<T*> will be chosen. 第二个是最佳匹配,因此将选择template<class T> struct A<T*> The other two will be stroke out. 其他两个将被淘汰。

# FAIL. Not specialized enough. Remove this from consideration.
//  template<class T> struct A {
//    typedef T type;
//    static const int value = 0;
//  };

# WIN. Use with T = char*.
    template<class T> struct A<T*> {
      typedef T type;
      static const int value = 12;
    };

# FAIL. Does not match char**.
// template<class T> struct A<T********> {
//   typedef T type;
//   static const int value = 1024;
// };

After performing substitution it becomes 执行替换后,它变为

struct A<char**> {
  typedef char* type;
  static const int value = 12;
};

and Line X and Y shall become X和Y线将变为

/*A<char**>::type*/ char* x;                       // <-- Line X
std::cout << /*A<char**>::value*/ 12 << std::endl; // <-- Line Y

(Note: (注意:

template<class T> class Vector

is not a partial specialization, 不是部分专业化,

template<class T> class Vector<T*>

this one is.) 这个是。)

When the compiler instantiates vector<char*> , it matches the following template: 编译器实例化vector<char*> ,它与以下模板匹配:

template<class T>
class vector<T*> {
  ...
};

For this template to produce a class vector<char*> it needs to be instantiated with T=char , and this is exactly what the compiler does. 为了使该模板生成类vector<char*> ,需要使用T=char实例化它,而这正是编译器所做的。

When the compiler sees the type vector<char*> it will not use T=char* to instantiate the template, because this would result in the wrong type vector<char**> . 当编译器看到类型为vector<char*> ,将不会使用T=char*实例化模板,因为这将导致错误的类型vector<char**> It will use T=char instead, which results in vector<char*> . 它将使用T=char代替,这将导致vector<char*> The compiler uses template parameters that will give the correct resulting type. 编译器使用模板参数,这些参数将给出正确的结果类型。

template<class t>
//teacher said that this partial specialization will handle all type of pointers
class vector<t*>
{
    ...
};

vector<char*> v;

Your confusion comes from assuming that the t in <class t> names the full argument that you give when instantiating. 您的困惑来自假设<class t><class t>命名了实例化时给出的完整参数。 Instead, the argument is matched against <t*> and so t = char . 而是将参数与<t*>匹配,因此t = char

For example, imagine the following specialization: 例如,想象以下专门化:

template <class T> struct Foo {};
template <class T> struct Foo<vector<T> > {}; //specialization for your vector

Foo<vector<int> > f;

Again it chooses the specialization, where vector<int> is matched against vector<T> and T = int . 再次选择特殊化,其中vector<int>vector<T>匹配,并且T = int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM