在处理结构模板中的整数时如何修复"X 不是类模板"?
How to fix 'X is not a class template' when dealing with ints in struct templates?
我无法编译我的代码,我正在尝试拥有一个类,该类使用接受 int 和参数包的结构模板存储有关类型的信息。
#include <tuple>
#include <cassert>
#include <iostream>
#include <cstring>
#include <vector>
template<int N, typename... Ts>
struct type_info_impl<N, Ts...> {
typedef typename std::tuple_element<N, std::tuple<Ts...>>::type type;
static const size_t size = sizeof(type);
};
template<typename... Types>
class type_info {
public:
type_info(){}
~type_info(){}
template<int N>
static constexpr size_t size(){
return type_info_impl<N, Types...>::size;
}
};
using types = type_info<bool, int, double>;
using namespace std;
int main()
{
cout << types::size<1>() << endl;
return 0;
}
应该输出数字"4",因为索引 1 处的类型 (int) 的大小为 4,但它抛出 "'type_info_impl' 不是类模板 结构type_info_impl {"
您用来定义type_info_impl
的语法是错误的。
用途
template<int N, typename... Ts>
struct type_info_impl<N, Ts...> { ...};
如果您尝试专用化类模板,则可以。若要定义基本类模板,请删除<N, Ts...>
位。只需使用
template<int N, typename... Ts>
struct type_info_impl { ...};
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 如何修复访问动态数组中结构中的字符串变量时"segmentation fault (core dumped)"错误
- 在结构函数之间传递文件路径 C++ 编辑:修复LNK2019错误
- 如何修复编译由结构组成的 std::p air 时出现的错误
- 如何修复 c++ 中结构的分割错误?
- 如何修复结构播放器对象中的矢量位置,以便它在 Main 中与 me.position 一起使用?
- 在处理结构模板中的整数时如何修复"X 不是类模板"?
- 如何修复初始化结构向量的错误
- 为什么调用定义的构造函数会导致结构声明中出现错误,我该如何修复它
- 修复已添加构造函数的结构的初始化