Std :: function vs function pointer

  • Are there any differences?

  • What is the best way to save / migrate?

    function<void(int)> fcn = 
                        [](int par) {std::cout<<"fcn: "<<par<<std::endl; };
    void(*fcn_a)(int) = 
                        [](int par) {std::cout<<"fcn_a: "<<par<<std::endl; };
    
    
    fcn(12);
    fcn_a(12);
    
+4
source share
2 answers

std::functionmore general - you can save any called object with the correct signature in it (function pointer, method pointer, object c operator()), and you can build std::functionusing std :: bind .

A function pointer can only accept functions with the correct signature, but it can be a little faster and can generate slightly smaller code.

+5
source

- , std::function. , std::function . , , operator(), .

OTOH, lambdas . , std::function , .

+5

Source: https://habr.com/ru/post/1570830/


All Articles