C++模板类型包装元组专业

发布于 2025-01-21 03:41:17 字数 531 浏览 3 评论 0原文

试图实现元组类型时,我会遇到一个空元组。
这是我使用的类型结构:

template <class T, class... Ts>
struct Tuple : public Tuple<Ts...> {};

template <class T>
struct Tuple {};

一旦我尝试添加一个无类型编译器的过载,编译器就会抱怨:类模板'tuple''的模板参数太少:

template <> struct Tuple<> {};

我想是因为这是因为首先以至少一种提供的类型声明了元组类型,并且编译器无法用不同的模板参数超载相同的类型,但是我想知道我如何在不完全重组我的代码的情况下解决此问题。

我的第一个想法是定义元组,例如template&lt; class ... ts&gt;结构元组{};首先,而不是添加其他过载,但是编译器比向大量模板参数投诉。

When trying to implement a tuple type i run into the problem an empty tuple.
This is the type structure i used:

template <class T, class... Ts>
struct Tuple : public Tuple<Ts...> {};

template <class T>
struct Tuple {};

As soon as i try to add an overload for no type the compiler complains: Too few template arguments for class template 'Tuple':

template <> struct Tuple<> {};

I guess it's because the Tuple type was declared with at least one provided type at first and the compiler can't overload the same type with a different set of template parameters, but i wonder how i could solve this problem without completely restructuring my code.
My first idea was to define the tuple like template <class... Ts> struct Tuple {}; first and than add the other overloads, but the compiler than complains for to much template arguments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2025-01-28 03:41:17

您的模板期望至少一个参数。您可以这样更改它以允许零或更多:

template <typename ... Ts>
struct Tuple;

template <>
struct Tuple<> {};

template <class T,class... Ts>
struct Tuple<T,Ts...> : public Tuple<Ts...> {};

int main()
{
    Tuple<int,int,double> t;
}

Your template expects at least one parameter. You can change it like this to allow zero or more:

template <typename ... Ts>
struct Tuple;

template <>
struct Tuple<> {};

template <class T,class... Ts>
struct Tuple<T,Ts...> : public Tuple<Ts...> {};

int main()
{
    Tuple<int,int,double> t;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文