2

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.

  • I've already seen a solution for this problem here (as from the title). There was s.th. like _double dspatch_ for parameterized macros. – πάντα ῥεῖ Feb 17 '14 at 20:37
  • Not possible as you stated it; as the preprocessor runs before the compiler, macros are mostly a textual substitution. If you explained more why you want this, we might be able to offer alternative solutions. – Thomas Feb 17 '14 at 20:37
  • @Thomas Yes I understand it's an X and Y problem, but essentially I'd rather not do `cmf(member_function)` for all the container's member functions. –  Feb 17 '14 at 20:40
  • I still don't get what you're trying to do. Where's the "container"? – Thomas Feb 17 '14 at 20:45
  • @Thomas Here is all the [code](http://coliru.stacked-crooked.com/a/b49206dd1eab4862). `cont` is the container. I'm trying to call `push_back` on `bar` which calls `cont.push_back`. One attempt is using a macro for each member function, `push_back`, `size`, etc. The other attempt doesn't require this but needs an infeasible cast. –  Feb 17 '14 at 20:47
  • Ah, so you're trying to "forward" member function calls to a member field? Why not simply have callers write `Bar::cont.push_back(42)` and be done with it? Alternatively (depending on the real situation), how about simply inheriting from `Container`? – Thomas Feb 17 '14 at 20:52
  • @remyabel You might try to use the tricks presented [here](http://stackoverflow.com/questions/2751870/how-exactly-does-the-double-stringize-trick-work), but I doubt you can use it for variadic macros the same way. May be you'll need to introduce some preprocessor meta-programming framework to achieve this (boost already provides such AFAIR). – πάντα ῥεῖ Feb 17 '14 at 20:52
  • @remabel here: [boost preprocessor metaprogramming](http://www.boost.org/doc/libs/?view=category_Preprocessor) – πάντα ῥεῖ Feb 17 '14 at 20:59
  • As an alternative approach, you could inherit from the container non-publically (thus evading all the "don't inherit from containers" issues) and publish those of its member functions which you want with `using` declarations. – Angew is no longer proud of SO Feb 17 '14 at 21:08

0 Answers0