【问题标题】:Two variadic templates for a single function? Part 2单个函数的两个可变参数模板?第2部分
【发布时间】:2013-05-23 18:02:20
【问题描述】:

继续:Two variadic templates for a single function?

我需要一个带有两个可变类型列表的函数。

例子:

template<typename... Types, typename... Args>
void function(Args&&... args) {
   ...
}

// usage
function</* types list for Types variadic */>(1,2,3/* arguments for Args */)

谢谢。

【问题讨论】:

  • 呃……有什么问题?就这样使用它。
  • 嗯...我还以为会错...
  • 在盲目提问之前总是尝试编译你的代码。

标签: c++ templates c++11 variadic-templates


【解决方案1】:

应该可以的:

#include <type_traits>
#include <tuple>

template<typename... Types, typename... Args>
void fxn(Args&&... args) {
   static_assert(
       std::is_same<std::tuple<Types...>, std::tuple<int, bool>>::value, "!");
   static_assert(
       std::is_same<std::tuple<Args...>, std::tuple<int, double, char>>::value, "!");
}

int main()
{
    fxn<int, bool>(42, 3.14, 'c');
}

Live example.

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 2023-03-13
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多