Answers for "templates in c++"

C++
1

template in c++

template <typename T>
void Swap(T &n1, T &n2)
{
	T temp;
	temp = n1;
	n1 = n2;
	n2 = temp;
}
Posted by: Guest on October-29-2020
1

template in c++

// template function
template <class T>
T Large(T n1, T n2)
{
	return (n1 > n2) ? n1 : n2;
}
Posted by: Guest on October-29-2020
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;
}
Posted by: Guest on May-04-2021
0

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;
    
}
Posted by: Guest on September-19-2021
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;
}
Posted by: Guest on December-02-2020
0

templates c++

template  < typename  T > 
inline  T  max ( T  a ,  T  b )  { 
    return  a  >  b  ?  a  :  b ; 
}

int  main () 
{ 
    // Isso chamará max <int> por dedução implícita do argumento. 
    std :: cout  <<  max ( 3 ,  7 )  <<  std :: endl ;

    // Isso chamará max <double> por dedução implícita do argumento. 
    std :: cout  <<  max ( 3.0 ,  7.0 )  <<  std :: endl ;

    // Isso depende do compilador. Alguns compiladores lidam com isso definindo uma 
    função de 
    modelo // como double max <double> (double a, double b) ;, enquanto em alguns compiladores // precisamos convertê-lo explicitamente, como std :: cout << max <double> (3,7,0); 
    std :: cout  <<  max ( 3 ,  7.0 )  <<  std :: endl ; 
    std :: cout  <<  max < double > ( 3 ,  7.0 )  <<  std :: endl ; 
    return  0 ; 
}
Posted by: Guest on June-16-2021

Browse Popular Code Answers by Language