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 one-to-many to our delegate

Currently, our wrappers store an instance to each type. This approach is often used with type erasure, but in our case, it prevents the ability to create many delegates for the same object (that is, no support for one-to-many). To fix this, we will store a pointer to an object in our wrappers instead of the object itself, as follows:

template<    typename T,    typename RET,    typename... ARGS    >class wrapper :    public base<RET, ARGS...>{    const T *m_t{};    RET (T::*m_func)(ARGS...);public:    wrapper(const T *t, RET (T::*func)(ARGS...)) :        m_t{t},        m_func{func}    { }    RET func(ARGS... args) override    {        return std::invoke(m_func, m_t, args...);    }};

As shown in the preceding, the only change we have made is we store ...

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