Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compile time check on existent class members or typedefs?

1 view
Skip to first unread message

Markus Werle

unread,
Jan 30, 2003, 6:44:47 PM1/30/03
to
Hi!

AFAIR some while ago there was a hot discussion whether some
dusty corners of the standard are exploitable to check at
compile time whether a certain class has a certain typedef
or nested class definition.

I also remember the discussion ended without a clear
answer on whether this is really possible with std::C++.

Since I did not find it again on google,
and in the hope that there _is_ some clear answer now:

Is it possible to check at compile time whether a
certain class contains a certain typedef?
E.g. testing for "value_type" in std::vector<double> ...

Any hints / URLs appreciated

best regards,

Markus


[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Shantanu Garg

unread,
Jan 31, 2003, 4:06:23 AM1/31/03
to

"Markus Werle" <numerical....@web.de> wrote in message
news:b1bkg8$11q4ul$1...@ID-153032.news.dfncis.de...

> Hi!
>
> AFAIR some while ago there was a hot discussion whether some
> dusty corners of the standard are exploitable to check at
> compile time whether a certain class has a certain typedef
> or nested class definition.
>
> I also remember the discussion ended without a clear
> answer on whether this is really possible with std::C++.
>
> Since I did not find it again on google,
> and in the hope that there _is_ some clear answer now:
>
> Is it possible to check at compile time whether a
> certain class contains a certain typedef?
> E.g. testing for "value_type" in std::vector<double> ...

I dont know whether this is a full proof answer or not but this should
help..

typedef char True;
typedef struct { char a[2]; } False;
template <class C> True hasTypeDef( typename C::value_type);
template <typename T> False hasTypeDef(...);

void main()
{
cout << (sizeof(hasTypeDef<std::vector<double> >(0))==sizeof(True)) ;
}

>
> Any hints / URLs appreciated
>

David Abrahams

unread,
Jan 31, 2003, 7:40:02 AM1/31/03
to
Markus Werle <numerical....@web.de> writes:

> Hi!
>
> AFAIR some while ago there was a hot discussion whether some
> dusty corners of the standard are exploitable to check at
> compile time whether a certain class has a certain typedef
> or nested class definition.
>
> I also remember the discussion ended without a clear
> answer on whether this is really possible with std::C++.
>
> Since I did not find it again on google,
> and in the hope that there _is_ some clear answer now:
>
> Is it possible to check at compile time whether a
> certain class contains a certain typedef?
> E.g. testing for "value_type" in std::vector<double> ...
>
> Any hints / URLs appreciated

>From the current Boost CVS:

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/boost/boost/boost/mpl/aux_/has_xxx.hpp?rev=HEAD&content-type=text/vnd.viewcvs-markup

#include <boost/mpl/aux_/has_xxx.hpp>

BOOST_MPL_HAS_TRAIT_NAMED_DEF(value_type)
...

#include <boost/static_assert.hpp>
#include <iterator>
BOOST_STATIC_ASSERT(!has_value_type<int*>::value);
BOOST_STATIC_ASSERT(has_value_type<std::iterator_traits<int*>::value_type>::value);

HTH,
--
David Abrahams
da...@boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

Joshua Lehrer

unread,
Jan 31, 2003, 12:32:28 PM1/31/03
to
"Shantanu Garg" <shanta...@mentor.com> wrote in message news:<b1d0af$10faov$1...@ID-90849.news.dfncis.de>...

> > Is it possible to check at compile time whether a
> > certain class contains a certain typedef?
> > E.g. testing for "value_type" in std::vector<double> ...
>
> I dont know whether this is a full proof answer or not but this should
> help..
>
> typedef char True;
> typedef struct { char a[2]; } False;
> template <class C> True hasTypeDef( typename C::value_type);
> template <typename T> False hasTypeDef(...);
>
> void main()
> {
> cout << (sizeof(hasTypeDef<std::vector<double> >(0))==sizeof(True)) ;
> }
>

close, but you made one mistake. In your example, 0 must be
convertible to the typedef type. If you had changed your example to
vector<empty>, you would have printed false, which is incorrect.
Instead, take a pointer to the typedef, as 0 is convertible to all
valid pointers:

template <class C> True hasTypeDef( typename C::value_type *);

joshua lehrer
factset research systems

0 new messages