How to cast forwarded arguments to std::invoke conditionally (if it’s a certain class) which came from std::forward’ed template (SFINAE?)?

I have created a class member invocation function, which does a delayed call, like:

func = NotifyAll(
    &get_file_listener,
    &GetFileListener::OnGetFileSuccess, container.c_str(), name.c_str());
// ... long delay and different scope
func();

But due to the nature of “execution occurring later” I realize that variables are captured by value will work, but obviously this won’t work for char* arguments, as in:

class ListenerInterface {
public:
    virtual ~ListenerInterface() {}
};

class GetFileListener: public ListenerInterface {
public:
    virtual void OnGetFileSuccess(const char* container, const char* name);
    virtual ~GetFileListener();
};

I have done this by templating and forwarding the member function and class type, then calling the function when required:

template<typename> struct extract_class_from_member_function_ptr;

template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::*)(ArgTypes...)> {
    using type = CLASS;
};

template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::* const)(ArgTypes...)> {
    using type = CLASS;
};

template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::*&)(ArgTypes...)> {
    using type = CLASS;
};

template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::* const&)(ArgTypes...)> {
    using type = CLASS;
};

template <typename T, class FuncT, class... ArgTypes>
void NotifyAllNow(T* one_time_specific_listener, FuncT&& Function, ArgTypes&&... Arguments) {
    using BaseT = extract_class_from_member_function_ptr<FuncT>::type;

    BaseT* casted_listener = (BaseT*)(one_time_specific_listener);
    //assert(casted_listener != nullptr);

    std::invoke(std::forward<FuncT>(Function), casted_listener, std::forward<ArgTypes>(Arguments)...);
}

template <typename... ArgTypes>
auto NotifyAll(ArgTypes&&... Arguments)
{
     return std::bind_front([](auto&&... args) {
        NotifyAllNow(std::forward<decltype(args)>(args)...);
        }, std::forward<ArgTypes>(Arguments)...);
}

I somehow need to extend the life of some parameters, I think something like an optional LifeExtender (or something else) could work:


class LifeExtender {
    std::optional<std::string> data;
public:
    LifeExtender(const char* const str) : data{} {
        if (str != nullptr) {
            data = std::string(str);
        }
    }

    LifeExtender(char* str) : data{} {
        if (str != nullptr) {
            data = std::string(str);
        }
    }

    LifeExtender(const std::string& str) : data{str} {}

    ~LifeExtender() {}

    operator const char* const() const noexcept {
        if (data) {
            return data->c_str();
        }

        return nullptr;
    }

    operator const char* () const noexcept {
        if (data) {
            return data->c_str();
        }

        return nullptr;
    }
};

So that I could just do:

func_fixed = NotifyAll(
    get_file_listener,
    &GetFileListener::OnGetFileSuccess, LifeExtender(container.c_str()), LifeExtender(name.c_str()), 0, 0);

And have everything work.

But now I’m stuck with trying to cast any LifeExtender object to char* ‘s (in this case by just calling () on them) in the templated invoke:

std::invoke(std::forward<FuncT>(Function), casted_listener, std::forward<ArgTypes>(Arguments)...);

I have no idea how to “for each forwarded argument” perform an std::conditional_t or make a separate specialization using std::enable_if to dereference / call () when the argument is of type LifeExtender, and just pass it along if not.

I think I should be taking inspiration form this post: Using std::forward on casted arguments

But templates are still difficult for me sometimes, I don’t think I know how to apply this.

What would be the correct way to solve this life extension issue in some neat way, so that it would allow me to keep calling member functions later (issue notifications)?

I unfortunately cannot change the parameters of the methods which receive the notifications as they are not controlled by me at all, I have to pass char*‘s unfortunately.
Changing everything to std::string would make this a non-issue, but I have to work with the cards I was dealt.

A full example can be found here:
https://coliru.stacked-crooked.com/a/15349c40ef71e904

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật