【问题标题】:Enable template function if class has specific member function如果类具有特定的成员函数,则启用模板函数
【发布时间】:2015-04-08 17:06:49
【问题描述】:

我编写了以下模板函数,它检查任意容器是否包含特定元素:

template<template<class, class...> class container_t, class item_t, class... rest_t>
bool contains(const container_t<item_t, rest_t...> &_container, const item_t &_item) {
    for(const item_t &otherItem : _container) {
        if(otherItem == _item) { return true; }
    }
    return false;
}

这适用于大多数容器。然而,对于所有类型的集合(和地图)来说,它是次优的,因为我们可以使用:

template<template<class, class...> class set_t, class item_t, class... rest_t>
bool contains(const set_t<item_t, rest_t...> &_set, const item_t &_item) {
    return _set.count(_item) > 0;
}

显然,由于模棱两可,我们不能同时使用这两个模板。现在我正在寻找一种方法,如果container_t 不提供count 成员函数,则使用std::enable_if 启用第一个模板,如果提供,则启用第二个模板。但是我不知道如何检查特定的成员函数(使用 C++11)。

【问题讨论】:

  • 如果您愿意将 boost 添加为依赖项,那么 boost.TTI 可以在 BOOST_TTI_HAS_MEMBER_FUNCTION 宏中找到您想要的内容
  • 您确定要这样做吗?在标准库中,当您进行线性搜索和使用更有效的机制时,它们让您准确地拼出您想要使用的find 形式,以便明确明确。附带说明一下,如果您确实想这样做,请务必使用 find 而不是 count,这只是避免过早悲观。
  • @SimonGibbons:谢谢,它看起来像我需要的。但是,我现在不希望依赖于 boost。
  • @MarkB:据我所知std::find 也使用不同的算法,这取决于我们是否有一个有序的容器,不是吗?至少en.cppreference.com/w/cpp/algorithm/find 写道,复杂性至多是线性的,即对于合适的容器来说可能小于线性。但是,如果我错了,请纠正我。
  • @Haatschii 该标准只是说“至多最后一个比较”,因此假设它对所有容器类型都是线性的。

标签: c++ templates c++11 sfinae


【解决方案1】:

C++14 功能,重新实现:

template<class...>struct voider{using type=void;};
template<class...Ts>using void_t=typename voider<Ts...>::type;

一个迷你元编程库:

template<class...>struct types{using type=types;};
namespace details {
  template<template<class...>class Z, class types, class=void>
  struct can_apply : std::false_type {};
  template<template<class...>class Z, class...Ts>
  struct can_apply< Z, types<Ts...>, void_t< Z<Ts...> > >:
    std::true_type
  {};
}
template<template<class...>class Z, class...Ts>
using can_apply = details::can_apply<Z,types<Ts...>>;

can_apply&lt; some_template, args... &gt; 继承自 true_type iff some_template&lt;args...&gt; 是一个有效的表达式(在直接上下文中)。

现在解决您的问题:

template<class T, class I>
using dot_count_type = decltype( std::declval<T>().count(std::declval<I>()) );

template<class T, class I>
using has_dot_count = can_apply<dot_count_type, T, I>;

而has_dot_count 是一个从true_type 继承的特征类,当且仅当T.count(I) 是一个有效的表达式。

namespace details {
  template<class C, class I>
  bool contains(std::false_type, C const& c, I const& i) {
    for(auto&& x:c) {
      if(x == i) { return true; }
    }
    return false;
  }
  template<class C, class I>
  bool contains(std::true_type, C const& c, I const& i) {
    return c.count(i) != 0;
  }
}
template<class C, class I>
bool contains( C const& c, I const& i ) {
  return details::contains( has_dot_count<C const&,I const&>{}, c, i );
}

它使用标签调度而不是 SFINAE。

顺便说一句,使用find 似乎比.count 更好。事实上,在一种情况下你应该使用.find 另一个find。在这两种情况下,您都应该使用using std::end; auto e = end(c);。

顺便说一句,MSVC 2013(我不知道 2015 年)不处理上面使用的这种 SFINAE。他们称之为“表达 SFINAE”。他们有自定义扩展来检测成员函数的存在。但那是因为它们远不符合 C++11 标准。

【讨论】:

  • VS 2015(至少预览版)doesn't support it as well
  • 有趣的方法。对我来说效果很好(GCC 4.8)。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
相关资源
最近更新 更多