I want to create a constant at compile time. This is what I mean:
template <int M = 31>
int fast_hash(int prev_hash, int c1, int c3)
{
enum {m3 = m*m*m;}
return m * prev_hash - m3 * c1 + c3;
}
In the above example, I could calculate m3 = m*m*m every time the function is called, but I think it would be cooler if this is done just once and during compile time. How can I do this?
It would best be done with C++11‘s
constexpr, but in C++03 you can still do it with a metafunction:It’s certainly cooler, but I think you are worrying about it too much and too early.