1

I was learning some basic cpp and I found out that we can pass lambda functions to a function accepting a std::function like below:

int someFunction(std::function<int(int, int)> sum) {
  return sum(1,3);
}

I was confused as to how the std::function is taking template types as "int(int, int)" generally I have only see classes taking types like someClass<int, int>.

How do we define classes that take the template types like "int(int, int)"?

I tried declaring classes like

template<typename A(typename B)> // --> this just gives an syntax error.
class SomeClass {};
Srinivas V
  • 93
  • 7
  • 1
    the form `TypeA(TxpeB,...)` is the type of a function. It is exactly the same as a regular type `int`. In my example, TypeA is the return type and TypeB is the argument. If you are using C++20, you can use the concept `std::invocable`. – Raildex Mar 12 '23 at 06:36
  • 2
    Do yourself a favor and don't guess your way through C++. Read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) instead. – Passer By Mar 12 '23 at 06:48

2 Answers2

0

At first, your example should not compile.

int someFunction(std::function<int(int)> sum) {
  return sum(1,3);
}

/*
std::function<int(int)>
              ^    ^
              |    |
           return  parameter
            type  

So, calling sum(1,3) would require function<int(int, int) as you are passing two parameters and returning a value.
*/

More examples

/*
The structure is "return_type(param1, param2, ..)" for the function and the following 
`[]` is used for variable capture (by value [x] or by reference [&x]) that resides out the function.
*/

// takes one param and returns nothing
function<void(int)> print = [](auto i) {
   court<<i<<endl;
}

// takes two params and returns the sum
function<int(int, int)> sum = [](auto i, auto j) {
   return i + j;
}

// takes one param and returns with added 2
function<int(int)> sum = [](auto i) {
   return i + 2;
}

How do we define classes that take the template types like "int(int, int)"?

template<typename T>
class Class {
  public:
    void call(T sum) {
      cout << sum(1, 3) << endl;
    } 
};

// usage
auto sum = [](auto a, auto b) { return a + b; };
auto instance = new Class<decltype(sum)>();
instance->call(sum);
tanmoy
  • 1,276
  • 1
  • 10
  • 28
  • Thanks for pointing out the error. I have corrected the same on the question. But the question remains the same, i.e. how do we create a class like std::function to take types like "int(int, int)". – Srinivas V Mar 12 '23 at 12:19
  • Updated. Please have a look. – tanmoy Mar 12 '23 at 21:40
0

I am answering my own question thanks to the comment by @Raildex. Basically the type int(int, int) in the question would refer to a function pointer that takes 2 ints and returns an int.

So an example class could just be:

template<typename T>
class SomeClass {
  public:
    void someMethod(T sum) {
      cout << sum(1, 3) << endl;
    } 
};

SomeClass someClass;
// the template type is not required here, but has been added just for clarity.
someClass.someMethod<int(int, int)>([](int a, int b){return a + b;});
Srinivas V
  • 93
  • 7