Topic: Enumerations as Template Parameters
Author: timw@dvc400.com (Tim Woodard)
Date: Mon, 11 Feb 2002 20:34:37 GMT Raw View
According to the standard (14.1.3), integral or _enumeration_ types
can be supplied as a template parameter. Based on this, I came up
with a little trick that in effect limits the values that can be
passed as a template parameter to values contained in an enumeration.
This can be useful for passing options as template parameters.
The following code compiles under MSVC 6 sp4, MWCW 7.2, g++ 3.0.2, and
como 4.3 online compiler:
// begin example
enum SomeEnum { A, B };
template <class EnumType, EnumType value>
struct TestStruct
{ };
int main()
{
TestStruct<SomeEnum, A> test_a;
TestStruct<SomeEnum, B> test_b;
}
// end example
Although I'm sure it's been done (hasn't it?), I've never seen this
technique used before, although it appears to be legal based on the
standard. Is this in fact a legal construct?
Now, when we throw partial specialization into the mix, things get
even more interesting. The following code comples under all of the
above mentioned compilers except MSVC (b/c of partial specialization,
of course) and como 4.3 online compiler:
// begin example
enum SomeEnum { A, B };
template <class T, class EnumType, EnumType value>
struct TestStruct
{ };
template <class EnumType, EnumType value>
struct TestStruct<int, EnumType, value>
{ };
int main()
{
TestStruct<int, SomeEnum, A> test_a;
TestStruct<float, SomeEnum, B> test_b;
}
// end example
como gives the following error:
"27156.c", line 9: error: the type of partial specialization template
parameter constant "value" depends on another template
parameter
template <class EnumType, EnumType value>
This would lead me to believe that the first example should not
compile either, as "value" would depend on another template parameter.
Is this just a peculiarity with partial specialization? What in the
standard prevents (or should allow) this?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Anthony Williams"<anthwil@nortelnetworks.com>
Date: Tue, 12 Feb 2002 17:50:24 GMT Raw View
"Tim Woodard" <timw@dvc400.com> wrote in message
news:3c6813ae.10355484@news.choiceone.net...
> The following code comples under all of the
> above mentioned compilers except MSVC (b/c of partial specialization,
> of course) and como 4.3 online compiler:
> enum SomeEnum { A, B };
>
> template <class T, class EnumType, EnumType value>
> struct TestStruct
> { };
>
> template <class EnumType, EnumType value>
> struct TestStruct<int, EnumType, value>
> { };
> como gives the following error:
>
> "27156.c", line 9: error: the type of partial specialization template
> parameter constant "value" depends on another template
> parameter
> template <class EnumType, EnumType value>
>
>
> This would lead me to believe that the first example should not
> compile either, as "value" would depend on another template parameter.
> Is this just a peculiarity with partial specialization? What in the
> standard prevents (or should allow) this?
It looks to me like Comeau is mis-applying the following section of the
standard:
14.5.4p9
"The type of a template parameter corresponding to a specialized nontype
argument shall not be dependent on a parameter of the specialization."
since the type of the "value" nontype parameter of your specialization is
dependent on the "EnumType" type parameter. However, I don't think this is a
problem, as the non-type argument is not specialized, since it names a
non-type parameter (14.5.4p8).
Maybe there's something else I haven't spotted, as Comeau is generally the
most standard-conformant.
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]