模板参数类型推演在函数对象中不起作用(Template argument type deduction won't work within a function object)
我有一个模板函数,带有模板化参数类型的另一个函数。
使用它时,我必须明确指定参数类型(1)否则不会编译(2)。
template<typename T> void process(const std::function<void(const T &)>& f) { // ... } process<Foo>( [&](const Foo& arg){/*...*/} ); // (1) Ok! // process( [&](const Foo& arg){/*...*/} ); // (2) Won't Work!让(2)工作有什么诀窍吗?
I have a template function taking another function with templated argument type.
When using it I have to specify the argument type explicitly (1) otherwise it won't compile (2).
template<typename T> void process(const std::function<void(const T &)>& f) { // ... } process<Foo>( [&](const Foo& arg){/*...*/} ); // (1) Ok! // process( [&](const Foo& arg){/*...*/} ); // (2) Won't Work!Is there any trick to let (2) work?
最满意答案
让编译器找出整个函数的确切类型:
template<typename T> void process(T&& f) { // ... }现在你的两个函数调用都可以。
Just let the compiler figure out the exact type of the whole function :
template<typename T> void process(T&& f) { // ... }Now both your function calls will work.
更多推荐
发布评论