【发布时间】: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<Foo *> 或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