[英]C++ boost enable_if question
我有什么方法可以简化以下陈述吗? (可能,使用boost::enable_if
) 。
我有一个简单的类结构- Base
的基类, Derived1
, Derived2
继承Base
。
我有以下代码:
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;
};
我想使用translator_between
一个模板特化来编写相同的语句。
我想要写的一个例子(伪代码):
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;
};
使用boost::enable_if
和boost::is_base_of
实现此目的的任何方法?
我不认为boost::enable_if
会有所帮助,因为SFINAE似乎更像是在函数重载之间进行选择。
您当然可以使用带有bool
参数的模板来优化选择:
#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
}
首先,你必须选择:
is_base_of
is_convertible
两者都可以在<boost/type_traits.hpp>
,后者更宽松。
如果您只是为某些组合阻止此类型的实例化,那么使用静态断言:
// 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
,因此您不需要enable_if
。
你可以在这里使用anable_if和这个宏来使它更具可读性:
#define CLASS_REQUIRES(...) typename boost::enable_if<boost::mpl::and_<__VA_ARGS__, boost::mpl::bool_<true> > >::type
然后你可以像这样定义你的类:
template <typename Class, typename Y, class Enable =
CLASS_REQUIRES(boost::is_base_of<Class, Y>)>
struct translator_between {
typedef some_translator<Class, Y> type;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.