我一直在听C++中的仿函数.有人可以给我一个关于它们是什么的概述以及在什么情况下它们会有用吗?
我有一个这样的函数要fmap为 C++ 实现:
// Given a mapping F from T to U and a container of T, return a container of U
// whose elements are created by the mapping from the original container's
// elements.
template <typename F, template <typename...> typename Container, typename T>
Container<std::invoke_result_t<F&, const T&>> Fmap(F&& f,
const Container<T>& input);
Run Code Online (Sandbox Code Playgroud)
这个想法是使用模板模板参数 ( Container) 来允许接受任何类似 STL 的容器。我尝试过的实际 STL 中的所有内容都工作正常,但我们的代码库中的自定义容器不起作用,因为它接受非类型模板参数
template <typename Key, int Foo = 256>
class MyContainer;
Run Code Online (Sandbox Code Playgroud)
这会导致 clang 替换失败:
template template argument has …Run Code Online (Sandbox Code Playgroud)