Introduction to c + + template

Function template

Article catalog


Code example

#include<iostream>
#include<string>

using namespace std;


//Use function template T here
template<typename T>
void swapValue(T & a, T & b) {
	T temp = a;
	a = b;
	b = temp;
}

//Multiple template parameter declaration methods
void func(T1 & t1,T2& t2) {
	cout << t1 << endl;
	cout << t2 << endl;
}

int main() {

	int a = 10, b = 20;
	cout << "a=" << a << "  b=" << b << endl;
	swapValue(a, b); //Type automatic derivation
	cout << "a=" << a << "  b=" << b << endl;

	float c = 12.34, d = 23.43;
	cout << "c=" << c << "\td=" << d << endl;
	swapValue<float>(c, d);//Explicitly specify type
	cout << "c=" << c << "\td=" << d << endl;

    func(a,c);
    func<int,float>(b,d)
    
	system("pause");
	return 0;
}

How to call function template

  • Automatic derivation type

    swapValue(a, b);
    
    //Automatic derivation does not have implicit type conversion (for example, char - > int)
    int a =10;
    char c='v';
    swapValue(a,c) //error
    
  • Explicitly specify type

    swapValue<float>(c, d);
    
    //Implicit type conversion occurs for explicit types
    int a =10;
    char c='v';
    swapValue<int>(a,c) //correct
    

matters needing attention

  • Automatically derived types must be consistent

    template<typename T>
    void swapValue(T & a, T & b) {
    	T temp = a;
    	a = b;
    	b = temp;
    }
    
    
    int a=10;
    float b=4.0;
    swapValue(a,b);//Error, inconsistent type
    
  • You must infer the type of the parameter to use

    template<typename T>
    void fun() {
    
    }
    
    int main(){
        fun(); //Error. Although the function does not use t, the compiler needs to infer the type of T when calling
        fun<int>();//Correct, specify a type explicitly
      
        return 0;
    }
    

Call rules of common functions and function templates

  • If both function templates and normal templates can be called, normal functions are preferred

  • Template functions can be forced to be called through an empty template parameter list

    fun<>();
    
  • Function templates can be overloaded

  • If the function template can match better, the function template is preferred

Template materialization

#include<iostream>
#include<string>


using namespace std;

class Person {
public:
	int age;
	string name;
};

template<typename T>
void swapValue(T & a, T & b) {
	T temp = a;
	a = b;
	b = temp;
}

//When passing in the Person type, use the following concrete function template
template<> void swapValue(Person & p1, Person& p2) {
	//int agetmp = p1.age;
	//string nametmp = p1.name;
	//p1.age = p2.age;
	//p1.name = p2.name;
	//p2.age = agetmp;
	//p2.name = nametmp;
	cout << "person swap is called..." << endl;
}



int main() {

	int a = 10, b = 20;
	cout << "a=" << a << "  b=" << b << endl;
	swapValue(a, b);
	swapValue<int>(a, b);
	cout << "a=" << a << "  b=" << b << endl;

	
	Person p1, p2;
	swapValue(p1, p2);

	system("pause");
	return 0;
}

Class template

Code example

class Data

#include<iostream>
#include<string>
template<class T>
class Data
{
	T data;
public:
	void setValue(T v) {
		data = v;
	}

	T getData() {
		return data;
	}
};

MainApp.cpp

int main(){
    
	Data<int> data;
	data.setValue(10);
	cout << data.getData() << endl;

	system("pause");
	return 0;
}

The difference between class template and function template

  • Class template has no automatic type derivation
  • Class template has default type parameter in template parameter list
template<class T=int>
class Data
{
	T data;
public:
	void setValue(T v) {
		data = v;
	}

	T getData() {
		return data;
	}
};



Data<> data; //Here, the default type parameter, int, is used. Int can be left blank, but it must have angle brackets < >

Class template as function parameter

  • Three ways
    • Specify the incoming type
    • Templating function parameters
    • Templating the entire class
template<class T=int>
class Data
{
	T data;
public:
	void setValue(T v) {
		data = v;
	}

	T getData() {
		return data;
	}
};


//1. Specify parameter type
void func1(Data<int>& d){
    
}


//2. Parameter type Templating
template<typename T>
void func2(Data<T>& d){
    // typeid() can view the types of runtime parameters
    cout<<typeid(T).name()<<endl;
}


//3. Template the whole class
template<typename T>
void func3(T& d){
    
}

Class template preparation by file

  • Preparation of ordinary documents
    • Class.h class declaration
    • Class.cpp Member function implementation
    • Include Class.h in main
  • For class templates, this can cause errors
    • Write all function implementations and declarations in one file and name it class.hpp

Posted on Fri, 26 Jun 2020 23:57:34 -0400 by karldesign