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! ]
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
>
> 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:
#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
> > 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