【问题标题】:C++: how can I overload a function so that it can take any functor, including pointer to member functions?C++:如何重载一个函数,以便它可以采用任何函子,包括指向成员函数的指针?
【发布时间】:2016-11-22 01:28:48
【问题描述】:

为了拓宽我对 C++11 的理解,我正在尝试编写函数式助手,看看是否可以让调用它们变得不那么冗长。现在,我正在尝试编写一个 some 函数,如果集合中的任何项目通过测试,该函数将返回 true。我希望它适用于任何集合类型,能够接受任何可调用对象,并且不需要模板参数。

基本上,我希望它能够编译以下代码:

// insert declaration of some here
#include <list>
#include <functional>

class Foo {
public:
    bool check() const { return true; };
};

class DerivedFooList : public std::list<Foo> {
public:
    DerivedFooList(std::initializer_list<Foo> args) : std::list<Foo>(args) {};
};

class DerivedFooPtrList : public std::list<Foo *> {
public:
    DerivedFooPtrList(std::initializer_list<Foo *> args) : std::list<Foo *>(args) {};
};

bool intCheck(int a) { return a == 1; }
bool fooCheck(const Foo &a) { return a.check(); }
bool fooPtrCheck(const Foo *a) { return a->check(); }

int main()
{
    Foo a, b, c;
    std::list<int> intList = {1, 2, 3};
    std::list<Foo> fooList = {a, b, c};
    std::list<Foo *> fooPtrList = {&a, &b, &c};
    DerivedFooList derivedFooList = {a, b, c};
    DerivedFooPtrList derivedFooPtrList = {&a, &b, &c};

    auto localIntCheck = [] (int a) { return a == 1; };
    auto localFooCheck = [] (const Foo &a) { return a.check(); };
    auto localFooPtrCheck = [] (const Foo *a) { return a->check(); };

    some(intList, [] (int a) { return a == 1; });
    some(intList, &intCheck);
    some(intList, localIntCheck);

    some(fooList, [] (const Foo &a) { return a.check(); });
    some(fooList, &fooCheck);
    some(fooList, localFooCheck);
    some(fooList, &Foo::check);

    some(fooPtrList, [] (const Foo *a) { return a->check(); });
    some(fooPtrList, &fooPtrCheck);
    some(fooPtrList, localFooPtrCheck);
    some(fooPtrList, &Foo::check);

    some(derivedFooList, [] (const Foo &a) { return a.check(); });
    some(derivedFooList, &fooCheck);
    some(derivedFooList, localFooCheck);
    some(derivedFooList, &Foo::check);

    some(derivedFooPtrList, [] (const Foo *a) { return a->check(); });
    some(derivedFooPtrList, &fooPtrCheck);
    some(derivedFooPtrList, localFooPtrCheck);
    some(derivedFooPtrList, &Foo::check);
    return 0;
}

请注意,如果集合的值类型是对象或对象指针,我希望能够将指向成员函数的指针作为第二个参数传递给some。这就是事情变得棘手的地方。

我的第一次尝试是这样实现它:

template <class T, class F>
bool some(const T &list, F &&func)
{
    for(auto item : list) {
        if (func(item)) {
            return true;
        }
    }
    return false;
}

template <template<class, class> class T, class U, class V, class W>
bool some(const T<U, V> &list, bool (W::*func)() const)
{
    return some(list, [=](U const &t){ return (t.*func)(); });
}

template <template<class, class> class T, class U, class V, class W>
bool some(const T<U *, V> &list, bool (W::*func)() const)
{
    return some(list, [=](U const *t){ return (t->*func)(); });
}

...但是如果集合不是 STL 集合,或者至少是一个带有两个模板参数的集合,它就不起作用。在我的示例中,使用 DerivedFooList 或 DerivedFooPtrList 将不起作用。

我的第二次尝试是这样的:

template <class T, class F>
bool some(const T &list, F &&func)
{
    for(auto item : list) {
        if (func(item)) {
            return true;
        }
    }
    return false;
}

template <class T, class U>
bool some(const T &list, bool (U::*func)() const)
{
    return some(list, [=](U const &t) { return (t.*func)(); });
}

现在适用于DerivedFooList,但不适用于std::list&lt;Foo *&gt; 或DerivedFooPtrList。

我的第三次尝试是这样的:

template <class T, class F>
bool some(const T &list, F &&func)
{
    for(auto item : list) {
        if (func(item)) {
            return true;
        }
    }
    return false;
}

template <class T, class U>
bool some(typename std::enable_if<std::is_class<typename T::value_type>::value, T>::type const &list, bool (U::*func)() const)
{
    return some(list, [=](U const &t) { return (t.*func)(); });
}

template <class T, class U>
bool some(typename std::enable_if<std::is_pointer<typename T::value_type>::value, T>::type const &list, bool (U::*func)() const)
{
    return some(list, [=](U const *t) { return (t->*func)(); });
}

暂时忽略这仅适用于具有名为value_type 的成员的集合,它仍然不允许我上面的示例编译。我认为(但不是 100% 肯定)原因是它无法在我希望使用它的情况下为 some 的后两个版本推断 T。

我非常努力地想看看是否有办法从 C++11 中得到我想要的东西,我怀疑有,但我想不通。我想要的有可能吗?如果有,我该怎么做?

【问题讨论】:

  • 只能到 C++11? C++17 怎么样?
  • 这个项目恐怕不能使用 C++17。

标签: c++ c++11 templates lambda template-argument-deduction


【解决方案1】:
template<class R, class F>
bool some( R const& r, F&& f ) {
  for(auto&& x:r)
    if (std::ref(f)(decltype(x)(x)))
      return true;
  return false;
}

std::ref 重载() 来实现INVOKE 概念,在C++17 中可以通过std::invoke 直接访问。您的要求似乎符合INVOKE 的概念。

如果x 是推导的auto&amp;&amp; 变量,则decltype(x)(x) 等价于std::forward 表达式。将其阅读为“将x 视为它被声明为的类型”。请注意,如果 x 是 auto 值,它会复制,这与 forward 不同。

INVOKE( pmf, ptr ) 和 INVOKE( pmf, ref ) 都可以工作。所以不需要在重载中做花哨的事情。

【讨论】:

  • 这就是诀窍!最重要的是,我不再需要重载我正在编写的任何函数。你能帮我理解为什么有必要使用decltype(x)(x)吗?如果我排除它会出现什么样的问题?
  • @guy 如果r 是T 的输入迭代器范围,x 将是一个左值,而ddeclt6pe(x)(x) 是一个右值。绑定到左值是无意义的、低效的并且通常是错误的。对于大多数常规容器,没有区别。但无妨。
  • 只需使用const 以提高清晰度和效率。避免过于微妙的事物和语言的阴暗角落。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多