Skip to Content
Advanced C++ Programming Cookbook
book

Advanced C++ Programming Cookbook

by Dr. Rian Quinn
January 2020
Intermediate to advanced
454 pages
11h 25m
English
Packt Publishing
Content preview from Advanced C++ Programming Cookbook

Adding support for non-member functions to our delegate

Finally, we need to modify the delegate to add support for non-member functions. Check out this example:

bool attack(int x, int y){    return x == 42 && y == 42 ? true : false;}

To do this, we simply need to add another wrapper as follows:

template<    typename RET,    typename... ARGS    >class fun_wrapper :    public base<RET, ARGS...>{    RET (*m_func)(ARGS...);public:    fun_wrapper(RET (*func)(ARGS...)) :        m_func{func}    { }    RET func(ARGS... args) override    {        return m_func(args...);    }};

As shown in the preceding, as with our original wrapper, we store a pointer to the function we wish to call, but in this case, we do not need to store a pointer to an object as there is no object (as this is a non-member ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern C++ Programming Cookbook

Modern C++ Programming Cookbook

Marius Bancila

Publisher Resources

ISBN: 9781838559915Supplemental Content