Re: About Int2Type

cppcraze wrote:

> Hi,
> Since loki help forum is not responded timely, I would like to ask a
> simple question here about Int2Type defined by Mr Alexandrescu.
> Following is definition of Int2Type:
> template<int v>
> struct Int2Type
> {
>      enum { value = v };
> };
> My question is: is the enumeration inside above struct necessary? I
> have tested the version with the enum removed and it also works, i.e.
> it can also do the compile-time dispatch according some compile-time
> integer. So why the enum is there? Is there any other consideration I
> missed?

The enum is not needed to make the template work.
But if you ever need to access the value that the type was created with,
you need the enum (or a similar, less convenient, mechanism) to do so.
So, the enum exists primarily to enable a reverse mapping from Int2Type
back to int.

> Thanks,
> Martin

Bart v Ingen Schenau
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

bart855 (270)
1/11/2008 9:58:56 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

0 Replies

Similar Articles

[PageSpeed] 55