C ++ domanda spinta enable_if
Domanda
Devo alcun modo per semplificare le seguenti affermazioni? (probabilmente, utilizzando boost::enable_if
) .
Ho una semplice struttura di classe -. Classe base Base
, Derived1
, Derived2
eredita da Base
Ho il seguente codice:
template <typename Y> struct translator_between<Base, Y> {
typedef some_translator<Base, Y> type;
};
template <typename Y> struct translator_between<Derived1, Y> {
typedef some_translator<Derived1, Y> type;
};
template <typename Y> struct translator_between<Derived2, Y> {
typedef some_translator<Derived2, Y> type;
};
Voglio scrivere la stessa dichiarazione utilizzando un modello di specializzazione delle translator_between
.
Un esempio (pseudocodice) di ciò che voglio essere in grado di scrivere:
template <typename Class, typename Y>
ONLY_INSTANTIATE_THIS_TEMPLATE_IF (Class is 'Base' or any derived from 'Base')
struct translator_between<Class, Y> {
typedef some_translator<Class, Y> type;
};
Un modo per raggiungere questo obiettivo utilizzando boost::enable_if
e boost::is_base_of
?
Soluzione
Per prima cosa, dovrete scegliere la vostra scelta tra:
-
is_base_of
-
is_convertible
entrambi possono essere trovate nel <boost/type_traits.hpp>
, quest'ultimo essendo più permissive.
Se con semplicemente evitare l'istanza di questo tipo per una combinazione, quindi utilizzare un assert statica:
// C++03
#include <boost/mpl/assert.hpp>
template <typename From, typename To>
struct translator_between
{
BOOST_MPL_ASSERT((boost::is_base_of<To,From>));
typedef translator_selector<From,To> type;
};
// C++0x
template <typename From, typename To>
struct translator_between
{
static_assert(boost::is_base_of<To,From>::value,
"From does not derive from To");
typedef translator_selector<From,To> type;
};
enable_if
Poiché non v'è un sovraccarico risoluzione che si svolgono qui, non è necessario.
Altri suggerimenti
I don''t penso boost::enable_if
aiuta, perché SFINAE sembra essere piuttosto di selezionare tra i sovraccarichi di funzioni.
Naturalmente, è possibile utilizzare i modelli con parametri bool
per affinare la scelta:
#include <boost/type_traits.hpp>
class Base {};
class Derived : public Base {};
template <class A, class B>
struct some_translator {};
template <typename A, typename B, bool value>
struct translator_selector; //perhaps define type as needed
template <typename A, typename B>
struct translator_selector<A, B, true>
{
typedef some_translator<A, B> type;
};
template <typename A, typename B>
struct translator_between
{
typedef typename translator_selector<A, B, boost::is_base_of<Base, A>::value>::type type;
};
int main()
{
translator_between<Base, int>::type a;
translator_between<Derived, int>::type b;
translator_between<float, int>::type c; //fails
}
È possibile utilizzare anable_if e questa macro qui per renderlo più leggibile:
#define CLASS_REQUIRES(...) typename boost::enable_if<boost::mpl::and_<__VA_ARGS__, boost::mpl::bool_<true> > >::type
Poi si può definire la classe come questa:
template <typename Class, typename Y, class Enable =
CLASS_REQUIRES(boost::is_base_of<Class, Y>)>
struct translator_between {
typedef some_translator<Class, Y> type;
};