Last Updated: February 25, 2016
·
1.086K
· dawehner

Fast square method using c++ templates

If you often need to calculate the square of a value you will realize that using pow(x, 2.0) from
math.h is not the fastest opportunity.

There are fastpow() implementions, some people use exp and log, and some people just write x*x in their code.

For the last case here is a small c++ template which reduces the effort to write x*x for long expressions of x:

template <class T>
inline T square(T value) {
    return value*value;
}