c++ template function
template <class T>
void swap(T & lhs, T & rhs)
{
T tmp = lhs;
lhs = rhs;
rhs = tmp;
}
c++ template function
template <class T>
void swap(T & lhs, T & rhs)
{
T tmp = lhs;
lhs = rhs;
rhs = tmp;
}
function template
template <class T>
void swap(T & lhs, T & rhs)
{
T tmp = lhs;
lhs = rhs;
rhs = tmp;
}
void main()
{
int a = 6;
int b = 42;
swap<int>(a, b);
printf("a=%d, b=%d\n", a, b);
// Implicit template parameter deduction
double f = 5.5;
double g = 42.0;
swap(f, g);
printf("f=%f, g=%f\n", f, g);
}
/*
Output:
a=42, b=6
f=42.0, g=5.5
*/
c++ class template
#include <vector>
// This is your own template
// T it's just a type
template <class T1, class T2, typename T3, typename T4 = int>
class MyClass
{
public:
MyClass() { }
private:
T1 data; // For example this data variable is T type
T2 anotherData; // Actually you can name it as you wish but
T3 variable; // for convenience you should name it T
}
int main(int argc, char **argv)
{
std::vector<int> array(10);
// ^^^
// This is a template in std library
MyClass<int> object();
// This is how it works with your class, just a template for type
// < > angle brackets means "choose" any type you want
// But it isn't necessary should work, because of some reasons
// For example you need a type that do not supporting with class
return (0);
}
cpp how to create an object of template class
template class Graph<string>;
template c++
template <class identifier> function_declaration;
template <typename identifier> function_declaration;
//Example:
template <class Type>
void Swap( Type &x, Type &y)
{
Type Temp = x;
x = y;
y = Temp;
}
template c++
template <class myType>
myType GetMax (myType a, myType b) {
return (a>b?a:b);
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us