A generic version of myabs(). : Template Overload « Generic « C++






A generic version of myabs().

A generic version of myabs().

#include <iostream>
using namespace std;

template <class X> X myabs(X val)
{
  return val < 0 ? -val : val;
}

int main()
{
  cout << myabs(-10) << '\n';    // integer abs

  cout << myabs(-10.0) << '\n';  // double abs

  cout << myabs(-10L) << '\n';   // long abs

  cout << myabs(-10.0F) << '\n'; // float abs

  return 0;
}



           
       








Related examples in the same category

1.Define template function for class typeDefine template function for class type
2.Overload a function template declaration.Overload a function template declaration.