Re: Detecting a static member variable
On 7 Mrz., 15:50, usenet only_tech_talk <usenet.tech.t...@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! ]