我决定尝试使用模板在 C++ 中编写功能性 map 实现,这就是我想出的:
template <
class U,
class V,
template <class> class T
>
class T<V> WugMap(
class T<U>::const_iterator first,
class T<U>::const_iterator second,
V (U::*method)() const)
{
class T<V> collection;
while (first != second)
{
collection.insert(collection.end(), ((*(first++)).*method)());
}
return collection;
}
现在一切都很好,甚至可以编译。问题是,我不知道如何实际调用它。
尝试天真的方法会产生以下错误:
prog.cpp:42: error: no matching function for call to
‘WugMap(__gnu_cxx::__normal_iterator<Container*, std::vector<Container,
std::allocator<Container> > >, __gnu_cxx::__normal_iterator<Container*,
std::vector<Container, std::allocator<Container> > >, int (Container::*)()const)’
据我所知,所有论点都是正确的。 gcc 根本没有建议任何替代方案,这让我相信我对 WugMap 的定义是可疑的,但它编译得很好,所以我很迷茫。任何人都可以指导我度过这个愚蠢的时期吗?
如果有人能提出更好的方法来编写这样的函数,支持使用包含任何类型对象的任何类型的集合,我会考虑对其进行更改。
我目前使用的是 Ideone,它使用的是 C++03、gcc 4.3.4。
附录 1
这在 C++11 中可行吗?有人暗示它是。我知道 C++11 中的模板支持不同数量的参数,所以我也会修改我的要求以适应它。我会付出一些努力来写一些东西,但与此同时,这里是我正在寻找的要求:
应该有一个像下面这样的签名:
C2<V, ...> map(const C1<U, ...>&, V (U::*)(...), ...)
这是获取一些集合 C1,其中包含类型 U 的元素,通过引用使用一些默认参数构造,并且还获取一些 U 的成员函数(返回 V 并获取一些未知类型的参数),然后按顺序获取要传递给成员函数的参数。该函数最终将返回一个 C2 类型的集合,其中包含 V 类型的元素并使用未知数量的默认参数进行初始化。
应该是可链接的:
vector<int> herp = map( map( set<Class1, myComparator>(), &Class1::getClass2, 2, 3), &Class2::getFoo);
如果我在使用它时不需要模板参数或任何其他额外的冗长内容,我会加分。
std::transform
很棒,但不可链接。
最佳答案
模板参数永远不能从嵌套类型中推导出来。即使可以从成员函数指针中推导出 U
和 V
,也无法推导出模板类型 T
。
在指向 ideone 的链接中明确指定模板参数(我在写上面的语句之前没有链接)也不起作用,主要是因为 std::vector
的模板参数不仅仅是一个类型 T
! std::vector
采用值类型和分配器类型。修复问题变得相当难看:
#include <vector>
#include <iostream>
using namespace std;
class Container
{
public:
Container() {}
Container(int _i) : i(_i) {}
int get_i() const {return i;}
int i;
};
template <
class U,
class V,
template <typename...> class T
>
T<V> WugMap(
typename T<U>::const_iterator first,
typename T<U>::const_iterator second,
V (U::*method)() const)
{
T<V> collection;
while (first != second)
{
collection.insert(collection.end(), ((*(first++)).*method)());
}
return collection;
}
int main()
{
vector<Container> containers;
for (int i = 0; i < 10; ++i) containers.push_back((Container(i)));
WugMap<Container, int, std::vector>(
containers.begin(), containers.end(), &Container::get_i);
}
关于c++ - 通过模板滥用的函数式 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12920863/