admin管理员组文章数量:1332377
Compiler can compile run
function without failure. But i dont know how to call this function.
#include <iostream>
#include <functional>
#include <utility>
#include <string>
template<class... Args>
void run(std::function<void(Args...)>&& f, Args&&... args) {
f(std::forward<Args>(args)...);
}
void greet(std::string name, int times) {
for(int i = 0; i < times; ++i) {
std::cout << "Hello, " << name << "!\n";
}
}
int main() {
std::function<void(std::string, int)> func = greet;
run<std::string, int>(greet, std::string("Bob"), 2); // Not working
run(func, std::string("Bob"), 2); // Not working
return 0;
}
Following code ok and works perfectly. I just couldn't find how the function in the above code could be called.
template<class Functor, typename... Args>
void run(Functor&& f, Args&&... args) {
f(std::forward<Args>(args)...);
}
Compiler can compile run
function without failure. But i dont know how to call this function.
#include <iostream>
#include <functional>
#include <utility>
#include <string>
template<class... Args>
void run(std::function<void(Args...)>&& f, Args&&... args) {
f(std::forward<Args>(args)...);
}
void greet(std::string name, int times) {
for(int i = 0; i < times; ++i) {
std::cout << "Hello, " << name << "!\n";
}
}
int main() {
std::function<void(std::string, int)> func = greet;
run<std::string, int>(greet, std::string("Bob"), 2); // Not working
run(func, std::string("Bob"), 2); // Not working
return 0;
}
Following code ok and works perfectly. I just couldn't find how the function in the above code could be called.
template<class Functor, typename... Args>
void run(Functor&& f, Args&&... args) {
f(std::forward<Args>(args)...);
}
Share
Improve this question
edited Nov 22, 2024 at 6:54
Qwe Qwe
asked Nov 22, 2024 at 6:28
Qwe QweQwe Qwe
4955 silver badges15 bronze badges
7
|
Show 2 more comments
2 Answers
Reset to default 2The source of deduction fail are two common oversights:
An rvalue refence of argument with type that isn't part of tempate argument list. Argument f
here is NOT a forwarding reference, it's an rvalue reference.
template<class... Args>
void run(std::function<void(Args...)>&& f, Args&&... args)
If you remove that &&, this particular use-case will compile:
run(func, std::string("Bob"), 2);
Other one is that function argument got type depending on argument pack and because of that the other case will not compile, as value of expression greet
is a pointer, not std::function
. It can be
run(std::function<void(std::string, int)>(greet), std::string("Bob"), 2);
If function greet
would take reference paramenters it results in inconsistent parameter pack deduction, e.g:
void greet(const std::string& name, int times)
would cause code to fail because of rvalue to lvalue reference match, which would require numerous work-arounds. Parameter packs and arguments depending on them require exact match.
There are good reasons why std::evoke
, a more general version of your run()
designed the way it was:
template< class F, class... Args >
std::invoke_result_t<F, Args...> invoke( F&& f, Args&&... args )
This isn't only type-mismatch free, it also allows to avoid run-time call dispatching built into std::function
. If that's not acceptable or run-time dispatch is required, it's better to use classic Visitor, Callback or Command patterns rather than try to use function pointer wrappers.
A common mistake is trying to use std::function
in templates. If this is used then its template parameters are not in deduce context. If you look at the standard library templates, you see they use Callable
template parameter. Use that parameter too.
#include <functional>
#include <iostream>
#include <string>
#include <utility>
template <class Callable, class... Args>
void run(Callable f, Args&&... args) {
f(std::forward<Args>(args)...);
}
void greet(std::string name, int times) {
for (int i = 0; i < times; ++i) {
std::cout << "Hello, " << name << "!\n";
}
}
int main() {
std::function<void(std::string, int)> func = greet;
run(greet, std::string("Bob"), 2); // Working
run(func, std::string("Bob"), 2); // Working
return 0;
}
Hello, Bob!
Hello, Bob!
Hello, Bob!
Hello, Bob!
https://godbolt./z/aMcbre7af
本文标签: cstdfunction variadic as parameterStack Overflow
版权声明:本文标题:c++ - std::function variadic as parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742294304a2448412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const
but the non const referencestd::string&
can't be bound to the prvaluestd::string("Bob")
. – 3CxEZiVlQ Commented Nov 22, 2024 at 6:51run(func, std::string("Bob"), 2)
compiles. You seem to answer your question. – 3CxEZiVlQ Commented Nov 22, 2024 at 6:56