4.11.2006

Q&A: #define and Templates

What's wrong with this macro: #define square(x) x*x ?

Answer: What if x is

  • 1+2 ( could be fixed with (x)*(x) )
  • ++a where a is an int.

Use templates instead.

template<class T>
inline const T& square(const T& x) { return x*x; }

Category: C++ Quant > Technology > C++

No comments:

Post a Comment