Вопрос

template<typename R, typename... Args1, typename... Args2>
void Func(R (*)(Args1...), Args2...)
{ // ...
}

template<typename R, typename... Args>
void Init(R(*)(Args...))
{
    // the second list is all of same types (a generic "any" type), just need same number as Args
    auto ptr = &Func<R, Args..., (Misc<Args>::type)...>;

    // ...
}


Init(&somefunc); // usage

Code kind of speaks for itself, I'm not sure how to separate the two so that they don't end up being concatenated to one variadic argument. I've tried using another class that holds a single single list of types and then Func would simply take 3 parameters but I keep getting explicit template error.

Это было полезно?

Решение

Such function declaration

template<typename R, typename... Args1, typename... Args2>
void Func(R (*)(Args1...), Args2...)
{ // ...
}

can only be instantiated by deducing the template parameter from the argument. But do you really need Args1? What about:

template<typename R, typename... Args2>
void Func(R, Args2...)
{ // ...
}

template<typename R>
R Init(R)
{
  // instantiate Func<>(R, int, float, int)
  auto ptr = &Func<R, int, float, int>;

  // ...
  return ptr;
}


void foo(int, float) {
//
}


int main() {
  Init(foo);
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top