|
Boost : |
Subject: [boost] How to determine if a function with a given signature is declared?
From: andrew.beck_at_[hidden]
Date: 2011-05-17 08:40:14
Hi,
Newbie question follows...
I've used BOOST_MPL_HAS_XXX_TRAIT_DEF in the past but recently needed to
determine if a function was defined with a given signature. Searching
boost::mpl, stackoverflow etc, I couldn't find anything to do what I
needed. I've attached my solution below. Basically, I'm wondering if there
is a boost::mpl solution or some other more elegant solution.
thanks,
andrew
#include <boost/mpl/bool.hpp>
#include <boost/mpl/assert.hpp>
#define XXX_DEFINED(symbol) \
template <typename T, T* = symbol> \
struct check_ ## symbol \
{ \
typedef void type; \
}; \
\
template <typename T, typename enable = void> \
struct symbol ## _defined : \
boost::mpl::false_ \
{ }; \
\
template <typename T> \
struct symbol ## _defined<T, typename check_ ## symbol<T>::type > : \
boost::mpl::true_ \
{ };
// declare a some_func(int) so we can see if its declared...
void some_func(int)
{}
// defines some_func_defined template
XXX_DEFINED(some_func);
BOOST_MPL_ASSERT(( some_func_defined<void(int)> ));
// some_func(double) hasn't been declared
BOOST_MPL_ASSERT_NOT(( some_func_defined<void(double)> ));
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk