tags:

views:

198

answers:

2

Hi,

Alot templated code looks like this:

template <typename T>
class foo
{
   enum { value = <some expr with T> };
};

An example can be seen here in the prime check program and I've seen it in a Factorial implementation once too.

My question is why use a nameless enum? Is there a particular reason to this? A static const int could work as well for example?

edit:

@Benoît: Thanks for the link, it provided the insight I was looking for!

+2  A: 

If I remember correctly, a static const would require you to allocate space by declaring and defining the variable whereas an unnamed enum doesn't.

Sean
+4  A: 

A static const variable would take up memory (like Sean said), whereas enums do not take any memory. They only exist in the compiler's world. At runtime they are just regular integers.

Other than that it would work, except for bad implementation of the standard by the compiler.

There is a thorough thread on the subject in boost mailing-list :

Benoît