еще вчера читал у Александревску, что определить, является ли тип enum невозможно, и тут встречаю в одной библиотеке следующее решение:
// Thanks to A. Alexandrescu for the idea
template <typename From, typename To>
struct IS_CONVERTIBLE { // can T be converted to U?
static char IsConvertible(To);
static int IsConvertible(...);
static From t;
static const int RET = sizeof(IsConvertible(t)) == 1;
};
// Metrowerks sizeof bug workaround
template <typename From, typename To>
const int IS_CONVERTIBLE<From, To>::RET = sizeof(IsConvertible(t)) == 1;
template <typename T> struct IS_ENUM
{
//Copyright for IS_ENUM
// (C) Copyright Steve Cleary, Beman Dawes, Howard Hinnant & John Maddock 2000.
// Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
private:
typedef typename ::boost::add_reference<T>::type r_type;
enum { NOT_ARITH = !(::boost::is_arithmetic<T>::value) };
enum { NOT_REF = !(::boost::is_reference<T>::value)};
enum { INT_CONVERT = (IS_CONVERTIBLE<r_type, ::boost::detail::int_convertible>::RET)};
public:
enum { RET = NOT_ARITH && NOT_REF && INT_CONVERT};
};
будет ли это всегда правильным решением?