Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-06-24 10:21:12


Kostya Altukhov wrote:
>
> Daniel Frey wrote:
>
> >This allows using sizeof_array safely, as it doesn't compile for
> >non-arrays. The only potential drawback I found is, that it's use is a
> >bit more complicated for static evaluation and sometimes, you need
> >typeof (hopefully in the next standard!?).
> >
> You can use the following trick:
>
> template<typename T, size_t N>
> const char (&sizer(const T (&)[N])) [N];

Interesting idea, but what compiler are you using? For me (gcc 2.95.2),
this doesn't work. I compiled:

#include <iostream>
using namespace std;

template< typename T, size_t N >
const char (&sizer(const T(&)[N]))[N];

int main()
{
   double x[] = { 1, 2, 3, 4, 5, 6 };
   typedef int X[42];

   enum {
      V1 = sizeof( sizer(X) ), // line 13, see below
      V2 = sizeof( sizer(x) )
   };

   cout << V1 << endl
        << V2 << endl;
}

and got:

t.cc:13: parse error before `)'

Which seems logical to me, as sizer is a function and you can't call a
function with a type as a parameter like you did. Or did I missed
something? I tried to merge the code, but even the (more useful) second
example doesn't work for me, as

   V2 = sizeof( sizer(x) )

couldn't deduce the parameters. It worked, when I wrote:

   V2 = sizeof( sizer< double, 6 >(x) )

which is rather pointless. Is this because of my compiler being too old?
Does the standard require a conforming compiler to be able to deduce the
parameters in this case?

Regards, Daniel

--
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk