infer lambda return type in template

javascrub

Hello i'm trying to create a function which will make a call to function provided as a parameter with some provided arguments and return its value, my current approach is as follows

  #include <functional>
  #include <iostream>
  #include <type_traits>


  template <typename Res,typename... T>
  auto func(typename std::common_type<std::function<Res(T...)>>::type f, T... values) -> decltype(f(values...)) {
    f(values...);
  }

  int fx(int x,int y){return x+y; }
  int main() {
>>  func([](int x, int y, int z) { std::cout << (x*y*z) << std::endl; }, 3, 6, 8);
    return 0;
  }

if i replace Res with void and remove the auto&&decltype it works, but somehow i'm not able to get the proper return type.

zch

Best and simplest way is to accept any functor, not just std::function:

template <typename F,typename... T>
auto func(F f, T... values) -> decltype(f(values...)) {
    return f(values...);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to infer correctly a return type for a template?

From Dev

infer template argument type from function return type

From Dev

infer template argument type from function return type

From Dev

Java - lambda infer type

From Dev

Writing a template function that evaluates a lambda function with any return type?

From Java

Infer return type of function based on type guard

From Java

Unable to infer complex closure return type with SwiftUI

From Dev

Infer class generic property method return type

From Dev

Can C++ templates infer return type?

From Dev

TypeScript return exact function infer type

From Dev

Scala method unable to infer return type

From Dev

Unknown return type in template

From Dev

Template parameter as return type

From Dev

Type casting to template return type

From Dev

Infer a return type of a method based on the type expected in call site?

From Dev

How to infer the type of the function that can return type depending on condition?

From Dev

Unable to infer complex closure return type; add explicit type to disambiguate?

From Dev

How to specify that template parameter is a class template, and infer its template type from another template parameter?

From Dev

How do I retrieve the type of a property from a Lambda and infer it on a list?

From Dev

How does compiler infer the delegate type from LAMBDA expression?

From Dev

How does compiler infer the delegate type from LAMBDA expression?

From Dev

Cannot infer a return type for Action delegate when used as a parameter

From Dev

Infer function generic type U from return value of passed function

From Dev

How to infer return type of Promise based on function arguments?

From Dev

Is it possible to infer return type of functions in mapped types in typescript?

From Dev

Cannot infer a return type for Action delegate when used as a parameter

From Dev

Specifying the lambda return type in Scala

From Dev

Return type of a C++ lambda

From Dev

bad return type in lambda expression

Related Related

  1. 1

    How to infer correctly a return type for a template?

  2. 2

    infer template argument type from function return type

  3. 3

    infer template argument type from function return type

  4. 4

    Java - lambda infer type

  5. 5

    Writing a template function that evaluates a lambda function with any return type?

  6. 6

    Infer return type of function based on type guard

  7. 7

    Unable to infer complex closure return type with SwiftUI

  8. 8

    Infer class generic property method return type

  9. 9

    Can C++ templates infer return type?

  10. 10

    TypeScript return exact function infer type

  11. 11

    Scala method unable to infer return type

  12. 12

    Unknown return type in template

  13. 13

    Template parameter as return type

  14. 14

    Type casting to template return type

  15. 15

    Infer a return type of a method based on the type expected in call site?

  16. 16

    How to infer the type of the function that can return type depending on condition?

  17. 17

    Unable to infer complex closure return type; add explicit type to disambiguate?

  18. 18

    How to specify that template parameter is a class template, and infer its template type from another template parameter?

  19. 19

    How do I retrieve the type of a property from a Lambda and infer it on a list?

  20. 20

    How does compiler infer the delegate type from LAMBDA expression?

  21. 21

    How does compiler infer the delegate type from LAMBDA expression?

  22. 22

    Cannot infer a return type for Action delegate when used as a parameter

  23. 23

    Infer function generic type U from return value of passed function

  24. 24

    How to infer return type of Promise based on function arguments?

  25. 25

    Is it possible to infer return type of functions in mapped types in typescript?

  26. 26

    Cannot infer a return type for Action delegate when used as a parameter

  27. 27

    Specifying the lambda return type in Scala

  28. 28

    Return type of a C++ lambda

  29. 29

    bad return type in lambda expression

HotTag

Archive