[ 
https://issues.apache.org/jira/browse/MESOS-7195?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15898743#comment-15898743
 ] 

Michael Park commented on MESOS-7195:
-------------------------------------

[~xujyan] Here's a small example that captures the limitation of variadic 
templates in this context:
{code}
#include <iostream>

struct S {
  void f(int) {}
  void f(int, int) {}
};

template <typename R, typename T, typename P, typename A>
void macro(R (T::*)(P), A) {}

template <typename R, typename T, typename P1, typename P2, typename A1, 
typename A2>
void macro(R (T::*)(P1, P2), A1, A2) {}

template <typename R, typename T, typename... Ps, typename... As>
void variadic(R (T::*)(Ps...) , As...) {}

int main() {
  macro(&S::f, 42);  // selects `void S::f(int)`
  macro(&S::f, 101, 202);  // selects `void S::f(int, int)`
  // variadic(&S::f, 42);  // error.
  // variadic(&S::f, 101, 202);  // error.
}
{code}

We have situations where there are overloaded member functions, and we happen 
to use
the # of arguments provided to narrow down the # of parameters we need to match.

The same trick doesn't work for variadic templates since the parameters and 
arguments are
both free-form. As far as I know, there's no way to express the same with 
variadic templates.

The macro form, of course, isn't good enough anyway, since it wouldn't work if 
{{f}} were to be
overloaded with different types and the same # of parameters, but we haven't 
run into that just yet.

By API changes, I mean that to make {{variadic}} work, we'll need to require 
the user to
pass something like:
{code}
variadic([](const S& s, auto... args) { s.f(args...); }, 101, 202);
{code}

This is not as generic as it needs to be, since it'll only call {{const}} 
functions.
To get the cv/ref qualifiers correct, we'd have to provide the proper overloads,
and maybe try to "hide" it with a macro... but it gets ugly...
{code}
variadic(MEM_FN(S, f), 101, 202);
{code}

where {{MEM_FN}} produce an overloaded function object.

Here's a rough sketch of how this could look: 
http://melpon.org/wandbox/permlink/BO8mf7r0CVr3akbu
Note that the sketch is written in C++14.

> Use C++11 variadic templates for process::dispatch/defer/delay/async/run
> ------------------------------------------------------------------------
>
>                 Key: MESOS-7195
>                 URL: https://issues.apache.org/jira/browse/MESOS-7195
>             Project: Mesos
>          Issue Type: Improvement
>          Components: libprocess
>            Reporter: Yan Xu
>
> These methods are currently implemented using {{REPEAT_FROM_TO}} (i.e., 
> {{BOOST_PP_REPEAT_FROM_TO}}):
> {code:title=}
> REPEAT_FROM_TO(1, 11, TEMPLATE, _) // Args A0 -> A9.
> {code}
> This means we have to bump up the number of repetition whenever we have a new 
> method with more args.
> Seems like we can replace this with C++11 variadic templates.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to