Made type of function pointer:

typedef bool (*BreakFunc)(IReader *reader); 

Some class has a method that takes a pointer to a function of type BreakFunc as a parameter. It looks like this:

 GaugeReader::addBreakFunc(BreakFunc func); 

In another class method, I call the addBreakFunc function addBreakFunc this:

 CalibProcedure::setBreakpoints() { GaugeReader r; r.addBreakFunc([](IReader *reader){ return false; }); } 

This code works without problems until the captured variables appear in the lambda. In this case, the compiler reports that it cannot find a suitable function addBreakFunc . That is, the captured variables affected the lambda so much that it ceased to conform to the BreakFunc type. Why did it happen? Are there other, more correct ways to force the addBreakFunc method to take a lambda?

C ++ 11 is used. I am sure that the correct methods exist, since such functions are in the Qt library, for example, QtConcurrent::run(тут лямбда) , but I did not find one that was needed in the heap of prototypes.

Maybe there are Qt tools to solve this problem?

  • GaugeReader :: addBreakFunc (std :: function <bool (IReader *)> _func); - Alexander
  • The variant with std :: function works, but the answers are still being accepted. Interested in whether there are similar classes in Qt. - maestro

4 answers 4

Here is an example of passing lambda as a function argument. Probably you need exactly that. Works in C ++ 11 and C ++ 14.

 #include <stdio.h> #include <functional> void invoke(std::function<int (int)> fun){ int res = fun(123); printf("result: %d \n", res); } int main(int argc, char *argv[]) { int a = 10; auto fun1 = [=](int x) { return x * a; }; auto fun2 = [&](int x) { return x * a; }; invoke(fun1); invoke(fun2); } 

    The type of lambda expression is an object of some anonymous class.
    If a lambda expression does not capture variables, then this class has a conversion operator to a pointer to a function; otherwise, it doesn’t.

    In other words, if there is such an opportunity, do not use function pointers, or pass the necessary variables in some other way.

    • one
      Add - instead of a pointer, you need to use either std::function , or make a method that takes a lambda, a template .. - сен

    Your assumptions are correct, so it is impossible to do.

    ( https://stackoverflow.com/questions/28746744/passing-lambda-as-function-pointer )

    It is not taken from the draft C ++ 14 standard section 5.1.2. 6 [expr.prim.lambda] says (emphasis mine):

    There is a possibility that she will be able to make a statement. This is the effect of the invoking of the function call operator.

    You can use std::function

      You can use templates.

       template <typename F> GaugeReader::addBreakFunc(F func); 
      • And why is no one a plus? The correct answer is. - VladD
      • @VladD, the answer is undoubtedly correct, but in this case the addBreakFunc method addBreakFunc be implemented in the header file. I would not like to do this. - maestro
      • @maestro: Why wouldn't you? With templates, you still have to do this - because you do not avoid templates in your program? - VladD
      • @VladD, no, I do not avoid templates, they are present in the program. Just the header file in this case (for me) looks clogged. Perhaps this is due to the fact that I have not learned to use them correctly, because the descriptions of templates in books and articles on the Internet are usually limited to a couple of examples. Here is an example of one use of templates in my code . Is this the correct (justified) use or is there another way to achieve the same effect? ps Perhaps it is better to go to the chat, but I do not know how. - maestro
      • @maestro: No, you're right, in this case the header looks terribly littered. But this is a problem of the language itself, so I used to put up with it. - VladD