dimanche 27 juin 2021

decltype and auto feature in C++11

why both auto and decltype. cant auto only solve the purpose ?? what is output of this program Can someone give an example how auto and decltype is used in templates

 template <class A, class B>
auto findMin(A a, B b) -> decltype(a < b ? a : b)
{
    return (a < b) ? a : b;
}
  
// driver function to test various inference
int main()
{
    // This call returns 3.44 of doubale type
    cout << findMin(4, 3.44) << endl;
  
    // This call returns 3 of double type
    cout << findMin(5.4, 3) << endl;
  
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire