From: usenet only_tech_talk on
Hello,

I tested the code below on a boost::array<> which defines
static const size_type static_size = N;
and it fails. See http://www.boost.org/doc/libs/1_42_0/doc/html/boost/array.html

By comparison, the commented out code for size(), not static_size
works. I use OSX 10.6, gcc 4.2.

Any suggestion to solve this would be appreciated. Thanks.

template<typename T>
struct has_static_size{
typedef typename T::size_type size_type;
typedef char yes;
typedef char (&no)[2];
typedef const size_type* sig;
// typedef size_type (const T::*sig)();

template<typename U,sig>
struct sfinae { };

template<typename U> static yes test(sfinae<U,
&U::static_size>*);
//template<typename U> static yes test(sfinae<U,&U::size> *);
template<typename U> static no test(...);

BOOST_STATIC_CONSTANT(
bool,
value = sizeof( test<T>(0) ) == sizeof(yes)
);

typedef boost::mpl::bool_<value> type;
};

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 7 Mrz., 15:50, usenet only_tech_talk <usenet.tech.t...(a)gmail.com>
wrote:
> I tested the code below on a boost::array<> which defines
> static const size_type static_size = N;
> and it fails. Seehttp://www.boost.org/doc/libs/1_42_0/doc/html/boost/array.html
>
> By comparison, the commented out code for size(), not static_size
> works. I use OSX 10.6, gcc 4.2.
>
> Any suggestion to solve this would be appreciated. Thanks.
>
> template<typename T>
> struct has_static_size{
> typedef typename T::size_type size_type;
> typedef char yes;
> typedef char (&no)[2];
> typedef const size_type* sig;
> // typedef size_type (const T::*sig)();
>
> template<typename U,sig>
> struct sfinae { };
>
> template<typename U> static yes test(sfinae<U,
> &U::static_size>*);
> //template<typename U> static yes test(sfinae<U,&U::size> *);
> template<typename U> static no test(...);
>
> BOOST_STATIC_CONSTANT(
> bool,
> value = sizeof( test<T>(0) ) == sizeof(yes)
> );
>
> typedef boost::mpl::bool_<value> type;
> };

This is a particular problem in the concrete
definition of boost::array's static_size as

enum { static_size = N };

and you take the address of an enumerator or any
other literal (Your test code only works for
lvalues like static const data member static_size).

Your problem is easy to fix: Just replace your
typedef "sig" by

typedef const size_type sig;

and your positive test overload by

template<typename U> static yes
test(sfinae<U, U::static_size>*);

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 7 mar, 14:50, usenet only_tech_talk <usenet.tech.t...(a)gmail.com>
wrote:
> Hello,
>
> I tested the code below on a boost::array<> which defines
> static const size_type static_size = N;
> and it fails. Seehttp://www.boost.org/doc/libs/1_42_0/doc/html/boost/array.html
>
> By comparison, the commented out code for size(), not static_size
> works. I use OSX 10.6, gcc 4.2.
>
> Any suggestion to solve this would be appreciated. Thanks.
>
> template<typename T>
> struct has_static_size{
> typedef typename T::size_type size_type;
> typedef char yes;
> typedef char (&no)[2];
> typedef const size_type* sig;
> // typedef size_type (const T::*sig)();
>
> template<typename U,sig>
> struct sfinae { };
>
> template<typename U> static yes test(sfinae<U,
> &U::static_size>*);
> //template<typename U> static yes test(sfinae<U,&U::size> *);
> template<typename U> static no test(...);
>
> BOOST_STATIC_CONSTANT(
> bool,
> value = sizeof( test<T>(0) ) == sizeof(yes)
> );
>
> typedef boost::mpl::bool_<value> type;
> };

This code is for testing static member variables.
Constant integral static member variables are however a special case
(you can't take their address), and need to be treated differently.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]