Recently learned C + + template, found that you can use template template parameter
Then I made up a little bit of code.
Template template parameter template <typename T, template<typename x> class CONE = std::d eque> class Mycla SS {Public:cone<t> A; MyClass () {}};
Discovery Compiler Error Error c3201:the template parameter list for class template ' std::d eque ' does ' "not match" template parameter Li St for template parameter ' CONE '
This error check, said: Do not have the template of the class you indiscriminately template.
But I am using the standard STL deque, there is no reason to come up with such a word
And then I tried it again.
Template template parameter template <typename t,class CONE = std::d eque<t> > class MyClass {public:cone A MyClass () {}};
Haha, this can be compiled through. No problem, then we can use this code instead of the above code. Looked for a bit. There will be security problems here. One kind of situation myclass<int, deque<int>> this is possible, another kind of situation myclass<double,deque<int>>. If we could put a variable of the first type of t into the cone, there would inevitably be a numerical conversion problem. The precision is lost here and can cause a bug. However, if Myclass<std::string,deque<const char *>> can cause a compilation problem. So how can we avoid this problem? Use the following code:
Template template parameter template <typename T,template<typename class x> class Cone> {MyClass One<t> A; MyClass () {}};
This allows you to keep the variable type T consistent with the type in cone. If I still want to have a default template, let me not have to join the STD every time::d eque. It's not back to the first piece of code. It will still cause error C3201 errors. And then I checked the Deque constructor in the standard STL library, deque requires two parameters such as: Deque<int, allocator<int>> The reason for this is that every time you apply for space, you are based on the size of the int , of course the STL allows you to define your own allocator. The following writing can be both secure and flexible:
Template template parameter template <typename t,typename A, Template<typename x,class, alloc> class CONE = std ::d eque> class MyClass {public:cone<t,a> A; MyClass () {}};
There are several points to be noted in the use of templates: One is template template parameter Template<template <typename t> class cone> Remember that this must be class. Generally we use Template,class and TypeName are no different. But here's the exception. TypeName is the name of the type (either the class name or the Aribtrary type base type), and class may be a template or class and struct. For example: std::d eque This is the template name, and std::d eque<int> This is the class name. So there is a certain difference between the two. In addition, some compilations will be able to compile my first code successfully, I am using vs2005. And in the second template TypeName X, and Class alloc is just a document parameter, is not practical. Just to mark the cone. You can think of the same as a function declaration.