|
Boost : |
From: Daniel James (daniel_at_[hidden])
Date: 2004-08-28 05:14:50
Peder Holt wrote:
> I solved this version in the vintage version of typeof.
>
> I ended up defining three macros for encoding and three macros for decoding:
> (probably only need 2)
>
> [snip]
It's possible to make unrecognised types integral by default. This is a
bit dodgy since it uses BOOST_PP_SEQ_HEAD in an unintended manner, and
uses BOOST_PP_IS_UNARY which is not part of the public interface and
currently not available on all compilers (this might change). Also,
BOOST_PP_IS_UNARY is unstable, Paul Mensonides wrote:
> Regarding the instability: the macro itself is stable, but the input is not
> which can foul up the internals. The library itself does a ton of extra
> scaffolding to force expansion order when it is important (for VC and MWCW < 9)
> it does this globally across the entire library. However, that scaffolding does
> not exist in user code (nor should it have to if at all possible), and IS_UNARY
> (etc.) cannot force an arbitrary amount of expansion to account for it. You'd
> be surprise how much stuff doesn't expand when it should on those preprocessors
> that happens to get picked up later. (You might have guessed that I really
> really hate those preprocessors.)
http://lists.boost.org/MailArchives/boost/msg69412.php
I've been doing this kind of thing in some of my own code, and it does
cause problems. Hopefully, in this case, since you're not doing much
processing on the arguments, BOOST_PP_CAT will do the sufficient
expansion. Anyway, here's the code:
#include <boost/preprocessor/control/iif.hpp>
#include <boost/preprocessor/detail/is_unary.hpp>
#include <boost/preprocessor/seq/seq.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/static_assert.hpp>
// Register the basic values:
#define BOOST_TYPEOF_typename (typename)
#define BOOST_TYPEOF_class (typename)
#define BOOST_TYPEOF_unsigned (unsigned)
// Check if the argument matches a registered type.
#define BOOST_TYPEOF_ARG_TYPE(x) \
BOOST_PP_IIF(BOOST_PP_IS_UNARY(BOOST_PP_CAT(BOOST_TYPEOF_, x)), \
BOOST_TYPEOF_ARG_TYPE_MATCHED, \
BOOST_TYPEOF_ARG_TYPE_UNMATCHED)(x)
#define BOOST_TYPEOF_ARG_TYPE_MATCHED(x) \
BOOST_PP_SEQ_HEAD(BOOST_PP_CAT(BOOST_TYPEOF_, x))
// The default value:
#define BOOST_TYPEOF_ARG_TYPE_UNMATCHED(x) integral
// Examples of use:
BOOST_TYPEOF_ARG_TYPE(typename) // = typename
BOOST_TYPEOF_ARG_TYPE(class) // = typename
BOOST_TYPEOF_ARG_TYPE(unsigned int) // = unsigned
BOOST_TYPEOF_ARG_TYPE(int) // = integral
BOOST_TYPEOF_ARG_TYPE(new_type) // = integral
// Add a custom type:
#define BOOST_TYPEOF_new_type (unsigned)
BOOST_TYPEOF_ARG_TYPE(new_type) // = unsigned
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk