What are the differences between Template functions, function templates, template classes, and class templates? What are the differences between Template functions, function templates, template classes, and class templates?

Source: Internet
Author: User

What are the differences between Template functions, function templates, template classes, and class templates? -Zhao baolong-blog Park

What are the differences between Template functions, function templates, template classes, and class templates? There are several such terms in C ++, but most of the time we use them is not correct, and they are almost replaced by each other. Below I want to thoroughly identify several terms, so as to avoid many conceptual obfuscation and usage errors.

These words are:

Function pointer -- pointer Function

Array Pointer -- pointer Array

Class template-template class

Function template-Template Function

In the end, we can make them actually named, and the name is justified.



1. function pointer -- pointer Function

Function pointers focus on pointers. A pointer pointing to a function. For example:

Int (* pf )();

Pointer functions focus on functions. Indicates a function, and its return value is a pointer. Example:

Int * fun ();



2. array Pointer -- pointer Array

Array pointers focus on pointers. A pointer pointing to an array. For example:

Int (* pa) [8];

The focus of pointer arrays is arrays. Indicates an array, which contains a pointer. Example;

Int * ap [8];



3. class template-template class (class template-template class)

Class templates focus on templates. A template is used to generate a class model. Example:

Template <typename T>

Class Vector

{

...

};

This Vector Template can generate many classes, such as Vector <int>, Vector <char>, Vector <int>, and Vector <Shape *> .......

The focus of the template class is the class. Indicates the class generated by a template. Example:

The preceding Vector <int>, Vector <char> ,...... All are template classes.

These two words are easy to confuse. I have seen many articles that make the mistake, even some English articles. Separating them is very important, so you can understand why the header file of the template is defined. h, the template member function implementation must also be written in the header file. h, instead of writing the class declaration in. in the H file, the definition of class is written in. cpp file. Refer to [34] Container classes and templates in C ++ FAQ Lite of Marshall Cline [34.12] Why can't I separate the definition of my templates class from it's declaration and put it inside. cpp file? URL address is http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12

I will extract several key paragraphs as follows, which are easy to understand in English:

In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types/whatever used to "fill in" the template. for example, if you're trying to use a Foo <int>, the compiler must see both the Foo template and the fact that you're trying to make a specific Foo <int>.

Suppose you have a template Foo defined like this:

Template <class T>
Class Foo {
Public:
Foo ();
Void someMethod (T x );
Private:
T x;
};

Along with similar definitions for the member functions:

Template <class T>
Foo <T>: Foo ()
{
...
}

Template <class T>
Void Foo <T >:: someMethod (T x)
{
...
}

Now suppose you have some code in file Bar. cpp that uses Foo <int>:

// Bar. cpp

Void blah_blah_blah ()
{
...
Foo <int> f;
F. someMethod (5 );
...
}

Clearly somebody somewhere is going to have to use the "pattern" for the constructor definition and for the someMethod () definition and instantiate those when T is actually int. but if you had put the definition of the constructor and someMethod () into file Foo. cpp, the compiler wocould see the template code when it compiled Foo. cpp and it wocould see Foo <int> when it compiled Bar. cpp, but there wowould never be a time when it saw both the template code and Foo <int>. so by rule above, it cocould never generate the code for Foo <int >:: someMethod ().

Example of a default template parameter:

Template <typename T = int>

Class Array

{

...

};

This is the first time I defined this template and used it:

Array books; // I think there are default template parameters, which is equivalent to Array <int> books

The above usage is incorrect and compilation fails because Array is not a class. The correct usage is Array <> books;

Here, Array <> is a specific class generated by a class template used for default template parameters.



4. function template-template function (function template-template function)

Function templates focus on templates. A template is used to produce functions. Example:

Template <typename T>

Void fun (T)

{

...

}

When using this function, you can explicitly produce template functions, such as fun <int>, fun <double>, and fun <Shape *> .......

You can also use the compiler to deduce template parameters during use to help you generate them implicitly (implicitly.

Fun (6); // implicitly generates fun <int>

Fun (8.9); // implicitly generates fun <double>

Fun ('A'); // implicitly generates fun <char>

Shape * ps = new Cirlcle;

Fun (ps); // implicitly generates fun <Shape *>



The focus of template functions is on functions. Indicates a function generated by a template. Example:

Fun <int> and fun <Shape *> generated by explicit or implicit (implicitly) above ...... All are template functions.

The template itself is a very huge topic. To make it clear, you need not an article, but a book. Fortunately, this book already has: David vanw.orde, nicola M. C ++ Templates: The Complete Guide written by josutis. Unfortunately, the Chinese mainland cannot buy the paper version, but an electronic version is circulating on the Internet.



The use of templates is very limited. Generally, they are just a model for generating classes and functions. In addition, there are very few fields used, so it is impossible to have any template pointer, that is, the pointer to the template, because in C ++, A template is a code production tool. In the final code, no template exists at all, and only the code of the specific classes and functions that the template has come up with exists.

However, a class template can also be used as a template parameter, policy-based Design in Andrei Alexandrescu's Modern C ++ Design is widely used.

Template <typename T, template <typename U> class Y>

Class Foo

{

...

};



From the discussion in the article, we can see that the name is very important. If you use the name improperly, it will cause a lot of trouble and misunderstanding. In actual programs, the naming of various identifiers is also a learning. In order to be clear and easy to understand, sometimes we still need to pay a certain price.

Note: In the terms in this article, the center of gravity of the language is behind, and the previous words are used as adjectives.

Wu Tong wrote in 2003.6.1

Last modified: 2003.6.16
This is what a buddy wrote on CSDN.
Http://www.csdn.net/develop/read_article.asp? Id = 19075
There are several such terms in C ++, but most of the time we use them is not correct, and they are almost replaced by each other. Below I want to thoroughly identify several terms, so as to avoid many conceptual obfuscation and usage errors.

These words are:

Function pointer -- pointer Function

Array Pointer -- pointer Array

Class template-template class

Function template-Template Function

In the end, we can make them actually named, and the name is justified.



1. function pointer -- pointer Function

Function pointers focus on pointers. A pointer pointing to a function. For example:

Int (* pf )();

Pointer functions focus on functions. Indicates a function, and its return value is a pointer. Example:

Int * fun ();



2. array Pointer -- pointer Array

Array pointers focus on pointers. A pointer pointing to an array. For example:

Int (* pa) [8];

The focus of pointer arrays is arrays. Indicates an array, which contains a pointer. Example;

Int * ap [8];



3. class template-template class (class template-template class)

Class templates focus on templates. A template is used to generate a class model. Example:

Template <typename T>

Class Vector

{

...

};

This Vector Template can generate many classes, such as Vector <int>, Vector <char>, Vector <int>, and Vector <Shape *> .......

The focus of the template class is the class. Indicates the class generated by a template. Example:

The preceding Vector <int>, Vector <char> ,...... All are template classes.

These two words are easy to confuse. I have seen many articles that make the mistake, even some English articles. Separating them is very important, so you can understand why the header file of the template is defined. h, the template member function implementation must also be written in the header file. h, instead of writing the class declaration in. in the H file, the definition of class is written in. cpp file. Refer to [34] Container classes and templates in C ++ FAQ Lite of Marshall Cline [34.12] Why can't I separate the definition of my templates class from it's declaration and put it inside. cpp file? URL address is http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.12

I will extract several key paragraphs as follows, which are easy to understand in English:

In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types/whatever used to "fill in" the template. for example, if you're trying to use a Foo <int>, the compiler must see both the Foo template and the fact that you're trying to make a specific Foo <int>.

Suppose you have a template Foo defined like this:

Template <class T>
Class Foo {
Public:
Foo ();
Void someMethod (T x );
Private:
T x;
};

Along with similar definitions for the member functions:

Template <class T>
Foo <T>: Foo ()
{
...
}

Template <class T>
Void Foo <T >:: someMethod (T x)
{
...
}

Now suppose you have some code in file Bar. cpp that uses Foo <int>:

// Bar. cpp

Void blah_blah_blah ()
{
...
Foo <int> f;
F. someMethod (5 );
...
}

Clearly somebody somewhere is going to have to use the "pattern" for the constructor definition and for the someMethod () definition and instantiate those when T is actually int. but if you had put the definition of the constructor and someMethod () into file Foo. cpp, the compiler wocould see the template code when it compiled Foo. cpp and it wocould see Foo <int> when it compiled Bar. cpp, but there wowould never be a time when it saw both the template code and Foo <int>. so by rule above, it cocould never generate the code for Foo <int >:: someMethod ().

Example of a default template parameter:

Template <typename T = int>

Class Array

{

...

};

This is the first time I defined this template and used it:

Array books; // I think there are default template parameters, which is equivalent to Array <int> books

The above usage is incorrect and compilation fails because Array is not a class. The correct usage is Array <> books;

Here, Array <> is a specific class generated by a class template used for default template parameters.



4. function template-template function (function template-template function)

Function templates focus on templates. A template is used to produce functions. Example:

Template <typename T>

Void fun (T)

{

...

}

When using this function, you can explicitly produce template functions, such as fun <int>, fun <double>, and fun <Shape *> .......

You can also use the compiler to deduce template parameters during use to help you generate them implicitly (implicitly.

Fun (6); // implicitly generates fun <int>

Fun (8.9); // implicitly generates fun <double>

Fun ('A'); // implicitly generates fun <char>

Shape * ps = new Cirlcle;

Fun (ps); // implicitly generates fun <Shape *>



The focus of template functions is on functions. Indicates a function generated by a template. Example:

Fun <int> and fun <Shape *> generated by explicit or implicit (implicitly) above ...... All are template functions.

The template itself is a very huge topic. To make it clear, you need not an article, but a book. Fortunately, this book already has: David vanw.orde, nicola M. C ++ Templates: The Complete Guide written by josutis. Unfortunately, the Chinese mainland cannot buy the paper version, but an electronic version is circulating on the Internet.



The use of templates is very limited. Generally, they are just a model for generating classes and functions. In addition, there are very few fields used, so it is impossible to have any template pointer, that is, the pointer to the template, because in C ++, A template is a code production tool. In the final code, no template exists at all, and only the code of the specific classes and functions that the template has come up with exists.

However, a class template can also be used as a template parameter, policy-based Design in Andrei Alexandrescu's Modern C ++ Design is widely used.

Template <typename T, template <typename U> class Y>

Class Foo

{

...

};



From the discussion in the article, we can see that the name is very important. If you use the name improperly, it will cause a lot of trouble and misunderstanding. In actual programs, the naming of various identifiers is also a learning. In order to be clear and easy to understand, sometimes we still need to pay a certain price.

Note: In the terms in this article, the center of gravity of the language is behind, and the previous words are used as adjectives.

Wu Tong wrote in 2003.6.1

Last modified: 2003.6.16
This is what a buddy wrote on CSDN.
Http://www.csdn.net/develop/read_article.asp? Id = 19075

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.