Boost logo

Boost :

From: Kostya Altukhov (kostya_at_[hidden])
Date: 2002-06-25 05:59:58


  Hello,

Daniel Frey wrote:

>template< typename T, size_t N >
>const char (&sizer(const T(&)[N]))[N];
>
You need to remove 'const' here. Then it obviously will not
work for types, but the following snippet works fine with
gcc 3.0.2, gcc 2.95.2, and online comeau:

#include <iostream>
using namespace std;

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

int main()
{
   double x[] = { 1, 2, 3, 4, 5, 6 };
   double y[] = { 1, 2, 3, 4, 5, 6, 7 };

   enum {
      V1 = sizeof( sizer(x) ),
      V2 = sizeof( sizer(y) )
   };

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

For typedefs such as "typedef int X[42];", a non-portable line that
works with mentioned compilers is "V1 = sizeof( sizer(*(X*)1 ))",
though I would not advise anybody to use that :-)

Best regards,
Kostya


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