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
  • 2 Compiler can compile run function without failure. I can't godbolt./z/sanfM6T7E – 3CxEZiVlQ Commented Nov 22, 2024 at 6:47
  • @3CxEZiVlQ Compiler can compile run function. comment all codes in main function. – Qwe Qwe Commented Nov 22, 2024 at 6:49
  • Now you have removed const but the non const reference std::string& can't be bound to the prvalue std::string("Bob"). – 3CxEZiVlQ Commented Nov 22, 2024 at 6:51
  • @3CxEZiVlQ I just asked, how can we call the run function? What I wrote in main is what I tried. – Qwe Qwe Commented Nov 22, 2024 at 6:53
  • Now you have fixed the root errors and run(func, std::string("Bob"), 2) compiles. You seem to answer your question. – 3CxEZiVlQ Commented Nov 22, 2024 at 6:56
 |  Show 2 more comments

2 Answers 2

Reset to default 2

The 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