如何使用可变模板声明std::元组

How I declare a std::tuple using variadic templates?

本文关键字:声明 std 元组 何使用      更新时间:2023-10-16

也许我在这里很天真,但我认为应该编译以下代码:

template <typename ... T>
struct Test {
        std::tuple<T> foo;
};
int main() {
        struct Test<int, long> test;
        return 0;
}

相反g++抱怨:

test.cpp:5: error: parameter packs not expanded with '...':
test.cpp:5: note:         'T'

我错过了什么?

您可以通过使用...:扩展包来实现这一点

template <typename... T>
struct Test
{
    std::tuple<T...> foo;
    //         ^^^^
};