471,584 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,584 software developers and data experts.

member template partial specialization

Hi folks,
I would like to know which clause of the standard rules out this:

template < typename eval >
struct recursive_template {

typedef typename eval::enum_type Enum;

template < Enum first, Enum last >
struct range {

range<first+1,last> data;

};// range

template < Enum val > // line 13
struct range<val,val> {};

}; // recursive_template
Comeau rejects this code saying:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 13: error: the type of partial specialization template
parameter constant "val" depends on another template parameter
template < Enum val >
^

I found [14.5.4/9]:

... - The type of a template parameter corresponding to a specialized
non-type argument shall not be dependent on a parameter of the
specialization. [Example:
template <class T, T t> struct C {};
template <class T> struct C<T, 1>; // error
template< int X, int (*array_ptr)[X] > class A {};
int array[5];
template< int X > class A<X,&array> { }; // error
?end example]

However, I somewhat fail to see how this applies in the case above since the
member template range does not seem to violate the restriction from
14.5.4/9. In particular, a non-member template version

typedef int Enum;

template < Enum first, Enum last >
struct range {

range<first+1,last> data;

};// range

template < Enum val >
struct range<val,val> {};

is fine.

I also considered whether there might some language in the standard that
implies that in

recursive_type<some_type>::range<1,20>

"eval" would be a template parameter of the member template range (because
that seems to be Comeaus's point of view), however, I did not find
something like that. Or is there something else in the standard that I am
missing here?

Thanks

Kai-Uwe Bux
Dec 9 '05 #1
1 2086
Kai-Uwe Bux wrote:
Hi folks,
I would like to know which clause of the standard rules out this:

template < typename eval >
struct recursive_template {

typedef typename eval::enum_type Enum;

template < Enum first, Enum last >
struct range {

range<first+1,last> data;

};// range

template < Enum val > // line 13
struct range<val,val> {};

}; // recursive_template
Comeau rejects this code saying:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 13: error: the type of partial specialization template
parameter constant "val" depends on another template parameter
template < Enum val >
^

I found [14.5.4/9]:

... - The type of a template parameter corresponding to a specialized
non-type argument shall not be dependent on a parameter of the
specialization. [Example:
template <class T, T t> struct C {};
template <class T> struct C<T, 1>; // error
template< int X, int (*array_ptr)[X] > class A {};
int array[5];
template< int X > class A<X,&array> { }; // error
?end example]

However, I somewhat fail to see how this applies in the case above since the
member template range does not seem to violate the restriction from
14.5.4/9. In particular, a non-member template version

typedef int Enum;

template < Enum first, Enum last >
struct range {

range<first+1,last> data;

};// range

template < Enum val >
struct range<val,val> {};

is fine.

I also considered whether there might some language in the standard that
implies that in

recursive_type<some_type>::range<1,20>

"eval" would be a template parameter of the member template range (because
that seems to be Comeaus's point of view), however, I did not find
something like that. Or is there something else in the standard that I am
missing here?


gcc 4.01 reports no comparable error. Instead, the program fails to
complie because the range class template recurses forever. The
range<val,val> specialization does not stop the recursion, at least
when val is a non-type parameter. I have found that wrapping an integer
in a type usually works in such situations. Something like:

template <int I>
struct Integer
{
typedef Integer<I+1> next;
};

To "increment" this type, simply refer to Integer<N>::next .

Greg

Dec 9 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Paul MG | last post: by
8 posts views Thread by Agent Mulder | last post: by
9 posts views Thread by Patrick Kutch | last post: by
9 posts views Thread by stephen.diverdi | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.