我在cppreference上看到了std::function::argument_type在C++ 17 中被弃用的东西.它背后的原因是什么?ISO WG21论文提出了哪些建议?
考虑以下功能:
template<class F>
void register_handler( F& f ) // any callable object
{
// find out T - the argument type of f
}
Run Code Online (Sandbox Code Playgroud)
这f是一个可调用的对象,接受一个参数.它可以是函数指针,也可以std::function是结果std::bind.
问题是,如何确定参数类型f并根据该类型执行某些操作?
一个简单的解决方法是明确地将类型添加到模板中,例如
template<class T, class F> // T is the argument type of F
void register_handler( F& f )
Run Code Online (Sandbox Code Playgroud)
但这似乎有点过分,因为类型F应该已经包含有关类型的必要信息T.
lambdas(匿名函数)的概念对我来说非常清楚.而且我知道类的多态性,运行时/动态调度用于根据实例的派生类型调用适当的方法.但是lambda究竟能够多态化吗?我是另一个Java程序员,试图学习更多有关函数式编程的知识.
c++ ×2
function ×2
c++11 ×1
c++17 ×1
deprecated ×1
haskell ×1
lambda ×1
polymorphism ×1
templates ×1