template in c++
template <typename T>
void Swap(T &n1, T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}
template in c++
template <typename T>
void Swap(T &n1, T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}
template in c++
// template function
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
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);
}
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;
}
cpp template
#include <iostream>
using namespace std;
class F
{
};
class S : public F
{
};
template<typename X, typename Y>
class check
{
class F { };
static F find( ... );
class T { F f[2]; };
static T find( Y* );
public:
enum
{
m = sizeof(T) == sizeof(find(static_cast<X*>(0)))
};
};
template <class Q, class R>
bool checkIf()
{
return check<Q, R>::m;
}
int main()
{
check <class F, class S> t (F,S);
cout << checkIf<class F, class S>() <<endl;
return 0;
}
template function in C++
// template specialization
#include <iostream>
using namespace std;
// class template:
template <class T>
class mycontainer {
T element;
public:
mycontainer (T arg) {element=arg;}
T increase () {return ++element;}
};
// class template specialization:
template <>
class mycontainer <char> {
char element;
public:
mycontainer (char arg) {element=arg;}
char uppercase ()
{
if ((element>='a')&&(element<='z'))
element+='A'-'a';
return element;
}
};
int main () {
mycontainer<int> myint (7);
mycontainer<char> mychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << endl;
return 0;
}
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