PrevUpHomeNext

day-15: c++ template


c++ template

Template is a template that is one thing to shape many things.

One thing: a c++ template; many things: many c++ classes.

c++ template is a generic programming basis of c++. c++ template is the basis of c++ type system.

template declaration

c++ template declaration:

template <typename type_t00>
... the-declared-name ...
template <typename type_t00, typename type_t01>
... the-declared-name ...

c++ template declaration declares a template that can be instantiated laterly.

the type_t00 and type_t01 are the template arguments, which are generic types, that can be any possible type. A generic type means it is not a specific type.

template instantiation

Passing a specific type to the template is called template instantiation.

the-declared-name<int> ...
the-declared-name<float, char> ...

The above instantiations pass int as type_t00, and pass float, char as type_t00, type_t01.

int, float, and char are specific types, which are not generic types.

c++ class template declaration and instantiation (example)

#include <iostream>

// template declaration
template <typename type_t00>
class my_class
{
private:
	type_t00 __value;
public:
	my_class(const type_t00 & value__):
		__value{value__} // It means initializing __value with value__ here.
	{
	}
	void print() const
	{
		std::cout << __value << std::endl;
	}
};

int main()
{
	// template instantiation
	my_class<int> objjpp{12345};

	objjpp.print();
}

This is template declaration:

template <typename type_t00>
class my_class

type_t00 is not specified, that will be waiting for specifying, it is generic.

This is template instantiation:

my_class<int> objjpp{12345};

The type_t00 is specified with int, by passing int to the template.

Serious Note:

Actually, the above statement contains three behaviors : template instantiation, object declaration, and object initialization.

my_class<int>
... objjpp
... objjpp{12345}

Just note what is template instantiation actually here.

template type parameter can be auto-deduced

#include <iostream>

template <typename type_t00>
class my_class
{
private:
	type_t00 __value;
public:
	my_class(const type_t00 & value__):
		__value{value__}
	{
	}
};

int main()
{
	my_class objjpp{12345};	// Instantiation: auto-deduced
}

The template parameter type type_t00 is auto-deduced as int, because 12345 is auto-deduced as type int.

template constructor

The examples above are using template class, that the template is generic for the whole class.

However, the template can be generic for a specific constructor.

template constructor example:

#include <iostream>

class my_class
{
public:
	template <typename type_t00>
	my_class(const type_t00 & v__)	// constructor 1
	{
		std::cout << "constructor 1 is called=> " << v__ << std::endl;
	}

	template <typename type_t00, typename type_t01>
	my_class(const type_t00 & v0__, const type_t01 & v1__)	// constructor 2
	{
		std::cout << "constructor 2 is called=> "
			<< v0__ << " " << v1__ << std::endl;
	}
};

int main()
{
	my_class obj11{23};	// Instantiation: call constructor 1
	my_class obj22{2.5, 'a'};	// Instantiation: call constructor 2
	my_class obj11_11{32};	// Instantiation: call constructor 1
}

Output:

constructor 1 is called=> 23
constructor 2 is called=> 2.5 a
constructor 1 is called=> 32

Note that
my_class is not a template class in this example,
you should not use my_class<int>, which is an error.

template method

The method of the class can be template too.

template method example

#include <iostream>

class my_class
{
public:
	template <typename type_t10>
	void say_something(const type_t10 & something) const
	{
		std::cout << "say: " << something << std::endl;
	}
};

int main()
{
	auto objjpp = my_class{};
	objjpp.say_something<int>(254444);	// type_t10 is passed as int
	objjpp.say_something<double>(254.444);	// type_t10 is passed as double
	objjpp.say_something('J');	// type_t10 is auto-deduced as char
}

output:

say: 254444
say: 254.444
say: J

Back to the starting line

Template is a template that is one thing to shape many things.

template <typename type_t00>
class my_class;

* One thing: one template: the template class my_class

* Many things: many classes: my_class<int>, my_class<double>, my_class<char>, ...

( my_class<int>, my_class<double>, my_class<char> are three different standalone classes.)







Written on Nov 12, 2024

Back to index

Index

cpp/c++

c++ std::exception:

std::cout.write(err.data(), err.size());

std::cout << std::endl;

caught:

  ===================================
  #  The c++ programming language.  #
  #                                 #
  #  Join c++ Discord: yZcauUAUyC   #
  #  Deck                           #
  ===================================

Home: cppfx.xyz

K


PrevUpHomeNext