Answers for "template structure 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
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