【问题标题】:Get Address of Variadic Variadic Template?获取可变参数模板的地址?
【发布时间】:2014-04-27 17:26:18
【问题描述】:
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

代码不言自明,我不确定如何将两者分开,以免它们最终连接到一个可变参数。我尝试使用另一个包含单个类型列表的类,然后 Func 只需要 3 个参数,但我不断收到显式模板错误。

【问题讨论】:

  • 确切的错误信息是什么?
  • 我不知道你想做什么。
  • @texasbruce 错误不明确,“非法使用显式模板参数”,不能部分特化模板函数...
  • “代码不言自明” --> 并非如此。
  • 我认为您在 Misc&lt;Args&gt;::type 之前需要一个 typename

标签: c++ templates variadic-templates


【解决方案1】:

这样的函数声明

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

只能通过从实参推导出模板形参来实例化。 但是你真的需要 Args1 吗?怎么样:

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);
}

【讨论】:

  • 再提供一个比较完整的用例,很难理解你要达到什么目的……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 2016-12-01
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多