Topic: Applying subscripting to types. Is it legal?
Author: whatiscpp@yahoo.com (John the newbie)
Date: Mon, 18 Mar 2002 02:10:40 GMT Raw View
Hi everybody,
is it legal to apply subscripting to types?
I ask this after reading the following code to compute the rank of an
array as a compile-time constant:
namespace ArrayRank
{
template <int i> struct Sized {
double array[i];
};
template <class T> struct rank_type
{
enum { value = 0 };
};
template <class T,int N> struct rank_type<T[N]>
{
enum { value = 1 + rank_type<T>::value };
};
template <class T> Sized< rank_type<T[1]>::value >
type_deduction_helper(T x[])
{ return Sized<rank_type<T[1]>::value>(); }
#define Rank(x) (sizeof(::ArrayRank::type_deduction_helper(x).array) \
/ sizeof(double))
}
This compiles fine, but OTOH I can't have
(double [4][7])[1] x;
to pass as a definition. What am I missing?
---
[ 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: "James Kuyper Jr." <kuyper@wizard.net>
Date: Mon, 18 Mar 2002 04:26:02 GMT Raw View
John the newbie wrote:
>
> Hi everybody,
>
> is it legal to apply subscripting to types?
> I ask this after reading the following code to compute the rank of an
> array as a compile-time constant:
>
> namespace ArrayRank
> {
> template <int i> struct Sized {
> double array[i];
> };
> template <class T> struct rank_type
> {
> enum { value = 0 };
> };
> template <class T,int N> struct rank_type<T[N]>
> {
> enum { value = 1 + rank_type<T>::value };
> };
> template <class T> Sized< rank_type<T[1]>::value >
> type_deduction_helper(T x[])
> { return Sized<rank_type<T[1]>::value>(); }
>
> #define Rank(x) (sizeof(::ArrayRank::type_deduction_helper(x).array) \
> / sizeof(double))
> }
>
> This compiles fine, but OTOH I can't have
>
> (double [4][7])[1] x;
>
> to pass as a definition. What am I missing?
T[N] and T[1] aren't subscipted types; they describe arrays of T, with
length N and 1, respectively. When you pass (double[4][7]) to that
template, the expression T[1] actually ends up meaning double[1][4][7]:
an array consisting of one element, of type T=(double[4][7]). If you
were expecting double[4][7][1], you're misunderstanding how array
declarations work. You don't need template parameters for this to come
up, the same issue comes up with ordinary typedefs.
---
[ 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 ]