As silly as it sounds, is this possible?
struct Bar {
void dummy() { }
#define act(name, ...) dummy(); \
// Do something inside Bar, not in main
}
bar.act(push_back, 42);
I presume not. But I'm attempting to somehow define a variadic macro as a member function of Bar
. The regular version looks like:
template <typename Func, typename... Args>
auto act(Func func, Args&&... args) -> decltype(func(std::forward<Args>(args)...)) {
return func(std::forward<Args>(args)...);
}
Which requires either two things: for me to use this macro to define every member function needed:
#define cmf(name) struct name \
{ \
template <typename... Args> \
auto operator()(Args&&... args) -> decltype(cont.name(std::forward<Args>(args)...)) { \
return cont.name(std::forward<Args>(args)...); \
} \
} name;
// ...
bar.push_back(42);
Or an infeasible cast:
bar.act(std::bind((void (std::vector<int>::*)
(const typename std::vector<int>::value_type& value))
&std::vector<int>::push_back, &bar.cont, std::placeholders::_1), 42);
My goal is to use this variadic macro to call cmf
for the name argument, and then use __VA_ARGS__
for the function arguments.