Resources for C ++ Templates

I am new to C ++ Templates and it is difficult for me to understand and debug them. What are some good resources for doing both /?

+4
source share
6 answers

I recommend the excellent C ++ Templates book - A Complete Guide from Vandevoorde and Josuttis.

+9
source

Alexandrescu is a template wizard. He has many reviews (many of which are freely available on his website and other sites) and some books.

Boost Documentation and Boost Cookbook is a good site to start with how you are comfortable with templates and feel like going to Boost.

+4
source

A template is just a way to reduce the amount of code you need to write when designing classes.

For example, the STL vector

std::vector<int> intList; std::vector<float> floatList; 

This will force the compiler to actually make the two classes backstage, similar to how you copied and pasted the code, but replaced all the int references with a float.

Without templates, you will need to create a vector class for each type that you want to process. Classes are more complex to use, but this is the easiest way to use, create and understand.

After compilation, the two sets of code for vector<int> and vector<float> completely separate, but the IDE will go through the template class when debugging.

+1
source

I have two quick tips that may save you some trouble in the future.

Templates must be defined and implemented in the headers because they are translated / created at compile time.

If you have the following code:

 // Foo.h template <typename T> class Foo { ... }; // Bar.cpp #include "Foo.h" void func() { Foo<int> fooInt; Foo<double> fooDouble; } 

When processing the Bar.cpp file, the compiler will β€œcreate” a new definition of the Foo class with each replaced T with an int type for the instance fooInt and a new definition of the Foo class with each replaced T a double type for the fooDouble instance.

In a nutshell, C ++ does not support template classes, the compiler imitates them, replacing types at compile time and creating normal classes. Thus, the compiled type fooInt and fooDouble are different.

Since they are broadcast at compile time and are not compiled separately, they must be defined and implemented in the headers.

If you implement the template class as a regular class (header and source file), the error created by the compiler (in fact, the linker) is that no method is implemented (because the source file is ignored).

You can still create declarations to avoid circular dependencies

It is simple that you should forward to declare them as follows:

 template <typename T> class Foo; 

And, as with normal forward ads, you must be sure to include all the necessary headers.

+1
source

They are designed to expand and modify as needed. Remember that this is a template, not a solution.

NTN

0
source

this is a very good and almost brief article to learn about C ++ templates
http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part

I will provide important details here:

1. function templates:

simple function template:

 template<class TYPE> void PrintTwice(TYPE data) { cout<<"Twice: " << data * 2 << endl; } 

note that "TYPE" can be any other valid name.

It is better to use a name that reflects the value of the type parameter and improves the readability of the code.

now you can call it like this:

 PrintTwice(5.5);//TYPE is 'float' PrintTwice(5);//TYPE is 'int' 

you can force the compiler to instantiate a function for the type that you pass explicitly:

 PrintTwice<double>(5); 

if you do not pass any type, the compiler determines the type.

you may have templates with multiple arguments and (optionally) a return type:

 template<class T> T Add(T n1, T n2) { return n1 + n2; } 

but note that you cannot, for example, pass n1 to int and n2 to double! both arguments must be of the same type, and the return type also applies to this type.

if you want to have a template function, perhaps another type argument:

 template<class T1, class T2> double Add(T1 t1Data, T2 t2Data) { return t1Data + t2Data; } 

and use it like:

 cout<<Add(5,7.0);//T1=int T2=double cout<<Add(5.5,7);//T1=double T2=int cout<<Add<double,double>(5,7);//T1=double T2=double by passing types explicitly 

When a function template accepts a template type, but not from its function arguments, for example:

 template<class T> void PrintSize() { cout << "Size of this type:" << sizeof(T); } 

You cannot call a function template such as:

 PrintSize(); 

Since this function template will require a specification of the template type argument, and it cannot be automatically output by the compiler. Correct call:

 PrintSize<float>(); 

2.class templates:

Definition

almost looks like:

 template<class T> class Item { T Data; public: Item() : Data( T() ) {} void SetData(T nValue) { Data = nValue; } T GetData() const { return Data; } void PrintData() { cout << Data; } }; 

but when using it, you must explicitly specify the type:

 Item<int> item1; 

the tricky part is that you cannot implement the declarations in the header file and the corresponding implementation in one or more source files (refer to the article to find out why)

Class Templates

Also, several non-piggy type template arguments allow:

 template<class T, int SIZE> class Array { static const int Elements_2x = SIZE * 2; void DoSomething(int arg = SIZE); // Non-const can also appear as default-argument... private: T TheArray[SIZE]; }; 

there are so many other parts and many more explanations that I cannot bring here.

I suggest you spend a few hours and read this article and the second part of this article. even if you think you know how templates work in C ++.

-1
source

All Articles