在主模板的声明中不允许使用模板参数列表

发布于 2025-02-01 19:35:01 字数 312 浏览 2 评论 0原文

我想编写一个模板类来计算元组的大小,但是编译时出现错误。

template <typename... Types>
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};

在此处输入图像描述

I want to write a template class to calculate the size of tuple,but error occur when compiling.

template <typename... Types>
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};

enter image description here

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

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

发布评论

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

评论(2

榕城若虚 2025-02-08 19:35:01

这看起来像是部分专业化,而没有先前的主模板,看起来像template&lt; typname&gt; struct tuple_size_ {};

This looks like a partial specialization without preceding primary template, which would look like template <typename> struct tuple_size_ {};.

驱逐舰岛风号 2025-02-08 19:35:01

问题是您提供了部分模板专业化,但尚未声明相应的主模板。也就是说,您没有首先要专业的类模板。换句话说,首先没有专业的东西(类模板)是没有意义的。

解决这需要为小学模板提供声明:

//primary template 
template<typename> struct tuple_size_; 

//now you can partially specialize the above primary template
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};

The problem is that you're providing a partial template specialization but haven't declared the corresponding primary template. That is, there is no class template that you want to specialize in the first place. In other words, it doesn't make sense to specialize something(the class template in your case) that isn't there in the first place.

To solve this you need to provide a declaration for the primary class template:

//primary template 
template<typename> struct tuple_size_; 

//now you can partially specialize the above primary template
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文