【问题标题】:template method matching derived type instead of base模板方法匹配派生类型而不是基类
【发布时间】:2015-05-12 22:30:17
【问题描述】:

我有一组运算符需要重写以用于表达式模板。我希望基类型的所有派生类都与基类型匹配。然后其他的东西会被泛型类型捕获。不幸的是,泛型类型在基类型之前获取派生类型。为了让事情变得美好和混乱,一切都被大量模板化,包括一些 CRTP。让我试着给出一个更简单的代码版本:

// Note: 'R' is used for return type
template <typename DerivedType, typename R>
class Base
{ // ...
};

template <typename E1, typename E2, typename R>
class MultOperation : public Base<MultOperation<E1, E2, R>, R>
{ // ...
};

template <typename T>
class Terminal : public Base<Terminal<T>, T>
{ // ...
};

// The broken operators:
template <typename T1, typename T2, typename R1, typename R2>
MultOperation<Base<T1, R1>, Base<T2, R2>, typename boost::common_type<R1, R2>::type>
operator*( Base<T1, R1> const& u, Base<T2, R2> const& v)
{
    return MultOperation<Base<T1, R1>, Base<T2, R2>, typename boost::common_type<R1, R2>::type>(u, v);
}

template <typename T1, typename T2, typename R1, typename R2>
MultOperation<Terminal<T1>, Base<T2, R2>, typename boost::common_type<T1, R2>::type>
operator*( T1 const& u, Base<T2, R2> const& v)
{
    return MultOperation<Terminal<T1>, Base<T2, R2>, typename boost::common_type<T1, R2>::type>(Terminal<T1>(u), v);
}

template <typename T1, typename T2, typename R1, typename R2>
MultOperation<Base<T1, R1>, Terminal<T2>, typename boost::common_type<R1, T2>::type>
operator*( Base<T1, R1> const& u, T2 const& v)
{
    return MultOperation<Base<T1, R1>, Terminal<T2>, typename boost::common_type<R1, T2>::type>(u, Terminal<T2>, v);
}

现在,我不能使用任何新的 C++ 功能。 (这是删除旧库的一些重构的一部分,因此我们可以升级到新的 cpp 标准。)不过,我可以使用 boost 的东西。我在想我的答案可能在于boost::enable_if 的东西,但我所有的尝试都导致了死胡同。现在,请记住,目标是表达式模板,所以我不能为传入的数据做任何转换。是的......它太复杂了......我希望你有一些魔法。

问题的简短版本: 如何让(1 * Derived) * Derived 匹配第一个运算符的operator(T, Base),然后匹配第二个运算符的operator(Base, Base)? 它目前匹配第一个罚款,然后第二个匹配一个 Base-generic 运算符,因为 T 不进行任何转换,因此比 Base 匹配得更好。

【问题讨论】:

  • 不是 class Terminal : public Base&lt;DerivedType&lt;T&gt;, T&gt; 应该是 class Terminal : public Base&lt;Terminal&lt;T&gt;, T&gt; 吗?
  • @MikeMB 是的。我现在已经改变了。我很快就写了这个问题,所以可能还有其他一些小问题。 @all 我开始认为隐式转换为Terminal 结合enable_if 可能会得到答案。如果出现不可预见的问题,仍在努力。
  • 那么您能否给出一组示例输入,即通过重载结果选择的运算符以及您希望选择的运算符?
  • 如果您有两个其他派生对象 A 和 B(数组或其他东西)并执行 1 * A * B 它应该将前两个与上面的第二个运算符匹配。这将返回MultOperation,它与B 应该匹配第一个运算符。目前,第二步将匹配更通用的第二个或第三个运算符。
  • 看起来我将不得不用原始类型重载大量运算符,而不是执行通用路由......我真的不想这样做。

标签: c++ templates boost enable-if expression-templates


【解决方案1】:

这是一个测试一个类是否是某种Base的特征:

template<class T>
struct is_some_kind_of_Base {
    typedef char yes;
    typedef struct { char _[2]; } no;

    template<class U, class V>
    static yes test(Base<U, V> *);
    static no test(...);

    static const bool value = (sizeof(test((T*)0)) == sizeof(yes));
};

然后约束你后面的两个operator*s,比如:

template <typename T1, typename T2,  typename R2>
typename boost::disable_if<is_some_kind_of_Base<T1>,
                MultOperation<Terminal<T1>, Base<T2, R2>, 
                              typename boost::common_type<T1, R2>::type> >::type
operator*( T1 const& u, Base<T2, R2> const& v) { /* ... */ }

Demo.

为了防止common_type 导致硬错误,我们需要推迟它的评估。

template <class T1, class T2, class R1, class R2>
struct make_mult_operation {
    typedef MultOperation<T1, T2, typename boost::common_type<R1, R2>::type> type;
};

template <typename T1, typename T2,  typename R2>
typename boost::disable_if<is_some_kind_of_Base<T1>,
                make_mult_operation<Terminal<T1>, T2, T1, R2> >::type::type
operator*( T1 const& u, Base<T2, R2> const& v) { /* ... */ }

Demo.

【讨论】:

  • 噢,是的,这是一个特点。看起来它会很好地工作。希望我也能为可爱的演示 +1 两次。
  • 其实这似乎有同样的问题... 可变参数列表会在基类型之前匹配。因此,子类将是no,不是吗?这是一个显示问题的经过调整的演示:demo
  • @Cory 不同的问题; common_type 的评估过早,导致硬错误。
  • 在我看来,common_type 是从采用一个泛型和一个基类的运算符调用的。这意味着禁用不会删除终端操作员版本。我的意思是,common_type 之所以失败,只是因为它正在获取MultOperation 而不是正确的R 模板参数,这是输入错误运算符的副作用。
  • @Cory 禁用在原始版本中启动得太晚了。原文中的逻辑类似于“计算 T (=MultOperation&lt;...&gt;);条件是否为真?如果是,则 SFINAE 并从重载集中删除;否则返回类型为 T”;问题是在这种情况下计算T 会导致硬错误。解决方法是推迟计算,直到我们检查了条件。
【解决方案2】:

我了解您的问题,即您希望为从给定基类型派生的类型专门化一个类模板。我会举一个没有那么多模板参数的例子。

正如您所建议的,这里的想法是通过enable_if 选择重载(我使用了std::enable_if 类,但您可以简单地将std:: 替换为boost:: 用于您的目的):

template<typename T, typename U>
struct is_derived_from_base
{
    static constexpr bool first = std::is_base_of<Base<T>, T>::value;
    static constexpr bool second = std::is_base_of<Base<U>, U>::value;

    static constexpr bool none = !first && !second;
    static constexpr bool both = first && second;
    static constexpr bool only_first = first && !second;
    static constexpr bool only_second = !first && second;
};

template<typename T, typename U, typename = typename std::enable_if<is_derived_from_base<T,U>::none>::type >
auto operator*(T const& t, U const& u)
{
    std::cout<<"Both T and U are not derived"<<std::endl;
    return MultOperation<T, U>(t,u);
}


template<typename T, typename U, typename = typename std::enable_if<is_derived_from_base<T,U>::only_first>::type >
auto operator*(Base<T> const& t, U const& u)
{
    std::cout<<"T is derived from Base<T>, U is not derived"<<std::endl;
    return MultOperation<Base<T>, U>(t,u);
}

template<typename T, typename U, typename = typename std::enable_if<is_derived_from_base<T,U>::only_second>::type >
auto operator*(T const& t, Base<U> const& u)
{
    std::cout<<"T is not derived, U is derived from Base<U>"<<std::endl;
    return MultOperation<T, Base<U> >(t,u);
}

template<typename T, typename U, typename = typename std::enable_if<is_derived_from_base<T,U>::both>::type >
auto operator*(Base<T> const& t, Base<U> const& u)
{
    std::cout<<"T is derived from Base<T>, U is derived from Base<U>"<<std::endl;
    return MultOperation<Base<T>, Base<U> >(t,u);
}

更多详情请看完整程序here。

【讨论】:

  • 正如我的问题中提到的,由于旧库,我无法使用 C++98 之后的任何内容。此更改与许多其他更改正在努力删除那些旧库,以便我们可以使用 11+ 进行编译,但这需要一段时间。因此,我不能使用默认模板参数。一旦我们移动到 11,我可能会更改代码看起来更像这个答案,但现在,我不能这样做。
  • 好吧,我认为“新功能”的意思是“没有 C++11”。可以编辑,但你明白了。
  • 尽管如此:将模板默认参数替换为返回类型,并将 std:: 替换为 boost:: 并且它可以工作。我更喜欢 boost::is_base_of 而不是手写版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
相关资源
最近更新 更多