• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Free course: C++ 11 to 20, Comparing different function types: Function pointer, std::function, functor, lambda

  • Thread starter Thread starter ptf
  • Start date Start date

ptf

Joined
3/29/21
Messages
20
Points
13
C++ 11 to 20:
Compare different function definitions as callback functions.
What are the pros and cons of these different ways of defining function types, which one we should choose.

I made this video for myself but also hoping this is helpful for others.

 
Which C++20 (trendy) syntax are you using? Most is C++11. Just saying.

Please post the code as an attachment so that the QN members can experiment with the code.
Many are up to speed with C++11 and beyond (including myself ):ninja:
 
Last edited:
@ptf
You also wrote this, which is a bit strange
According to some research billions of dollars are due to fact of memory leak in C/C++, which requires very high skill on the developers. When it comes to quantitative finance, same thing. No matter how many good students you teach, they make mistakes and maybe the next financial crisis could be because of C++ and memory leak.

Why C++? Why not Rust? Rust is more safer and as fast as C.

Are you for or against C++?

Experienced C++ developers do not seem to have these problems. They add to the profitability of the organisation.
 
Last edited:
@ptf
You also wrote this, which is a bit strange
According to some research billions of dollars are due to fact of memory leak in C/C++, which requires very high skill on the developers. When it comes to quantitative finance, same thing. No matter how many good students you teach, they make mistakes and maybe the next financial crisis could be because of C++ and memory leak.

Why C++? Why not Rust? Rust is more safer and as fast as C.

Are you for or against C++?

Experienced C++ developers do not seem to have these problems. They add to the profitability of the organisation.
I'm not for nor against C++. It's always right by keeping an open minded, humbled position, IMO.
 
C++:
// Youtube video: https://www.youtube.com/watch?v=5yb9TEoMr2A
#include <iostream>

int regular_function(int arg) { return arg; }

int three_arg_function(std::string arg1, double arg2, int arg3) { return arg3; }

class function_class {
   public:
    int three_arg_function(std::string arg1, double arg2, int arg3) { return arg3; }
};

typedef int (*func_ptr)(int);
void using_func_ptr(func_ptr callback) { callback(1); }

using func_type = int(int);
void using_func_type(func_type callback) { callback(1); }

using stdfunc_type = std::function<int(int)>;
void using_std_func(stdfunc_type callback) { callback(1); }

struct functor_type {
    int operator()(int arg) { return arg; }
};
void using_functor(functor_type callback) { callback(1); }

auto global_cap = 15;

int main() {
    auto cap = 12;
    auto ff = std::bind(three_arg_function, "something", 2.0, std::placeholders::_1);
    function_class fclass;
    auto clz_ff = std::bind(&function_class::three_arg_function, &fclass, "something else", 3.0,
                            std::placeholders::_1);
    // using func_ptr
    using_func_ptr(regular_function);
    using_func_ptr([](auto arg) { return arg * global_cap; });
    // using_func_ptr([&cap](auto arg) { return arg * cap; });  // compile error
    // using_func_ptr([&](auto arg) { return arg * cap; });     // compile error
    // using_func_ptr([=](auto arg) { return arg * cap; });     // compile error
    // using_func_ptr([cap](auto arg) { return arg * cap; });   // compile error
    // using_func_ptr(ff);                                      // compile error
    // using_func_ptr(clz_ff);                                  // compile error
    // using_func_ptr(functor_type());                          // compile error

    // using func_type
    using_func_type(regular_function);
    using_func_type([](auto arg) { return arg * global_cap; });
    // using_func_type([&cap](auto arg) { return arg * cap; });  // compile error
    // using_func_type([&](auto arg) { return arg * cap; });     // compile error
    // using_func_type([=](auto arg) { return arg * cap; });     // compile error
    // using_func_type([cap](auto arg) { return arg * cap; });   // compile error
    // using_func_type(ff);                                      // compile error
    // using_func_type(clz_ff);                                  // compile error
    // using_func_type(functor_type());                          // compile error

    // using functor_type
    using_functor(functor_type());
    // using_functor(regular_function);                           // compile error
    // using_functor([](auto arg) { return arg * global_cap; });  // compile error
    // using_functor([&cap](auto arg) { return arg * cap; });     // compile error
    // using_functor([&](auto arg) { return arg * cap; });        // compile error
    // using_functor([=](auto arg) { return arg * cap; });        // compile error
    // using_functor([cap](auto arg) { return arg * cap; });      // compile error
    // using_functor(ff);                                         // compile error
    // using_functor(clz_ff);                                     // compile error

    // using std_func
    using_std_func(regular_function);
    using_std_func([](auto arg) { return arg * global_cap; });
    using_std_func([&cap](auto arg) { return arg * cap; });
    using_std_func([&](auto arg) { return arg * cap; });
    using_std_func([=](auto arg) { return arg * cap; });
    using_std_func([cap](auto arg) { return arg * cap; });
    using_std_func(ff);
    using_std_func(clz_ff);
    using_std_func(functor_type());

    return 0;
}
 
I'm not for nor against C++. It's always right by keeping an open minded, humbled position, IMO.
Unfortunately, it's the way you are saying it that gets me, It is incorrect and naive.
But that's my experience and opinion. Try to tone it down a bit and be more factual.
Anyhoo, no problem.
 
Last edited:
Back
Top