Elaborate template non-type parameters

Nontype template argument elaborated

non-type parameter on the template, there are many online content, C ++ primer only about one forth, but the details are not clear enough. Now I go as far as possible from their own perspective for everyone to describe non-type parameter details. If you want to understand the non-template type parameter and can read the contents of the book C ++ template, the relevant explanation in 4.1, 8.3.3, 13.2 have.

 

In addition to defining the type of template parameters, we can also define non-type template parameters.

What is non-type parameter? As the name suggests, it is to represent a fixed type constant rather than a type.

 

To cite a simple example (template class can be used with non-template function type parameter)


//例子1:
template<class T,int MAXSIZE> class List{
private:
T elems[MAXSIZE];
public:
Print(){ cout<<"The maxsize of list is"<<MAXSIZE; }
}
List<int,5> list;
list.Print();//打印"The maxsize of list is 5"

This type of fixation is limited, only plastic, pointers, and references to a non-type parameter, and bound to the parameter of the argument must be a constant expression, that is, compile the results can be confirmed.

 

It should be emphasized that, for non-type parameters we define two aspects to look

1. limited template parameter, i.e. template <> inside the parameters

2. defining the template argument, i.e. when instantiating <> inside the parameters

 

 

One by one to explain the limitations of the following non-type parameters

1. As a non-float type parameter may not include float, double. Specific reasons may be historical reasons, perhaps the future will support C ++ float.

2. Class not be used as a non-type parameter.

3. As a non-string type parameters can not

4. The shaping can be converted into plastic can be used as a type parameter, such as int, char, long, unsigned, bool, short (internal data enum declaration may be passed as an argument to the int, but generally not as parameter)

The pointer to the referenced object or function (left reference value) as a parameter can be

 

 

Here to explain the limitations of non-type arguments

1. The argument must be compile-time constant expressions, can not use non-const local variables, partial objects and dynamic objects address

2. Non-Const global pointer, the global object, global variables (there may be a special case below) is not a constant expression.

3. As has been done parameter defined, string, floating point expression nor even as a non-constant type argument

Note: The constant expression is basically a literal as well as modified variable const

 

// Example 2:
Template <class T, int the MAXSIZE> class List {
Private:
T the elems [the MAXSIZE];
public:
void the Print () {COUT << "List of The MAXSIZE of IS" << the MAXSIZE;}
};

const int num1 = 9;; // global variable
static int num2 = 9;; // global variable
const int num3 = 9;; // local variables

List <int, num1> list; // correct
List <int, num2> list; // error
List <int, num3> list; // correct

// pointer to look on a string comparison and specific examples
@ example. 3:
Template <const char * name>
class pointerT {

};
char a [] = "saaa" ;; // global variable
char a2 [] = "saaa" ;; // local variables inside a function written in the main
char * b = "saaa"; // global variable
char * const c = "saaa" ;// global variable, the top pointer, pointer constant


pointerT < "testVarChar"> p1; // error

pointerT <a> p2; // correct
pointerT <a2> p22; // error, local variables can not be used as non-type parameters
pointerT <b> p3; // error, error C2975: invalid "pointerT" template parameters, constant expressions should compiler
pointerT <c> P4; // error, error C2970: "c": expression involving objects with internal links can not be used as non-type parameters
// pointer on the pointer and constants can refer blog 

Const usage summary (quickly distinguish pointer constant and constant pointer)

Here we may have a few questions

①. In the end Why can not string as an argument?

A: We see the above p1 template argument is "testVarChar", but when we also declare such a template instance in another compilation unit (.cpp file), the two "testVarChar" address may be different, will deliver a different delivery address when the compiler is passed to the template, resulting in two templates are examples of two different and incompatible types. This is the problem of supporting a string of lies. (There may be a deeper realization of the principle involved template)

 

②. Variables b and c as a template argument for why the error different?

A: First argument to explain b, where b is seen as a pointer, is a global pointer, but he is not a constant expression, not so b. Let us look at c, c compared to b to a const qualifier, indicating that pointer is a constant. However const keyword is a rather special, he has an internal link properties (on the connecting link understanding of C ++ reference blog: Link meaning and are linked within C ++), that is only visible within the definition of this variable document, not Links will cause errors when mixed different compilation unit.

This feature is for the template, but there is a problem, as described problems ①, as each compilation unit may have a variable c, resulting in at compile time, instantiate multiple c, and c address also different, causing two examples of templates are two different and incompatible types.

 

③ Why a variable as an argument can?

A: I have read some books, for example in the case have not been used const modified above in addition to the formation of extern extern constchara [] = "saaa"; this form of statement, used in conjunction with extern and const can really suppress the const internal property.

This can be seen as an array of a type here (a further understanding of the relationship between the reference array and the pointer is blog 

char * itoa (int, char * , int); The second argument is obviously a char *, but can not Why is "char *"? ) ,
Then we see him not only to avoid different ① instantiation address the problem (as is the globally unique), but also avoids the problem of internal links const bring, so this one probably is the result of optimized compiler the result of.

Reprinted: https://blog.csdn.net/u012999985/article/details/50780311

Guess you like

Origin www.cnblogs.com/boldness2012/p/12359004.html