C 尺寸不适合模板
C++ sizeof does not work with template
本文关键字:不适合 更新时间:2023-10-16
可能的重复:
C2070-非法操作数
为什么不编译以下代码的模板版本,而当删除(未使用)模板时,相同的确切代码会编译?
不编译:
template <typename T>
class Foo {
public:
static double const vals[];
int run ();
};
template <typename T>
double const Foo<T>::vals[] = { 1, 2,3 };
template <typename T>
inline
int Foo<T>::run () {
return sizeof(vals); // error C2070: 'const double []': illegal sizeof operand
}
编译:
class Foo {
public:
static double const vals[];
int run ();
};
double const Foo::vals[] = { 1, 2,3 };
inline
int Foo::run () {
return sizeof(vals);
}
因为视觉工作室被打破。请参阅:http://connect.microsoft.com/visualstudio/feedback/details/759407/can-not-get-size-size-of-size-of-static-aray-aray-aray-defined-in-class-class-template
可能的(未经测试的)解决方法是使用类似的东西:
private:
template<typename T, int size>
int get_array_length(T(&)[size]){return size;}
然后使用:
int Foo::run() {
return get_array_length(vals) * sizeof(double);
}
相关文章:
- 我适合在我的构建系统中使用 shell 脚本而不是AX_CHECK_COMPILE_FLAG吗?
- 指数近似不适合小输入或大输入
- 使用"JSON for Modern C++"库检测整数不适合指定类型?
- 为什么 std:: setw 和 std::hex 不适合下面的代码?
- 将 32 位浮点数和不强制转换的 32 位整数与双精度进行比较,当其中一个值可能太大而无法完全适合另一种类型时
- 此方法是否不适合在 std::unordered_map 中使用 2D 坐标作为键
- 在x86_64模式下,64 位数字不适合寄存器整数
- 为什么我的数据不适合CUDA纹理对象
- 以下等级表达式不适合Boost Regex
- C 尺寸不适合模板
- QTableView长标题字符串不适合
- 建设者的设计模式不适合我
- 从一个程序到另一个程序的管道输出不适合这个特定的程序
- QSortFilterProxyModel不适合我
- 不适合设备的ioctl
- c++ - OOP实现链表,我不知道为什么添加到末尾不适合我,请建议
- 初始目录不适合CFileDialog
- 重定位值不适合24位
- 为什么内联SQL不适合包含在应用程序中
- OpenCV中的视频显示窗口大小不适合视频