不能将可变参数传递给嵌套可变模板调用

Cannot Pass Variable Args to Nested Variadic Template Call

本文关键字:调用 嵌套 参数传递 不能      更新时间:2023-10-16

编译器:TDM-GCC 5.1.0 (SJLJ Unwinding)

我有一个问题传递变量数量的类型参数到静态可变模板函数内部的模板方法调用。我已经尝试了所有的语法变化,但它无法编译,所以我只能假设我做错了。

设置如下:

#include <iostream>
template <class T>
struct Foo
{
    template <class...>
    static void test()
    {
        std::cout << "Foo<T>::test<...>() called.";
    }
};

template <class T, class... Args>
void bar()
{
    Foo<T>::test<Args...>();  //error happens here
}
int main()
{
    bar<int, int>();
}

这给出了编译错误:expected primary-expression before '...' token .

我认为包扩展看起来像Args...,但这似乎不工作在这里。

你需要告诉解析器依赖的test是一个模板:

template <class T, class... Args>
void bar()
{
    Foo<T>::template test<Args...>();  //error happens here
            ^^^^^^^^^
}
演示