问题描述
以下代码对我来说很好:
The following code looks fine to me:
#include <stdio.h>
template <typename T>
struct A
{
static float m_kA[];
};
template <typename T>
float A<T>::m_kA[] = {1.0f, 2.0f, 3.0f};
int main()
{
printf("%d
",
sizeof(A<unsigned int>::m_kA) /
sizeof(A<unsigned int>::m_kA[0]));
return 0;
}
但是当我用 VC9 编译时出现以下错误
But when i compile with VC9 i get the following error
error C2070: 'float []': illegal sizeof operand
我希望这段代码能够编译.我错过了什么吗?有谁知道解决这种奇怪行为的方法(请注意,没有模板的完全相同的东西可以正常编译并输出 3).
I would expect this code to compile. Am i missing something? Does anyone know a way to fix this strange behavior (note that the exact same thing without the template compiles fine and outputs 3).
请注意,删除模板不是一个选项,我制作这个示例是为了重现我在代码中遇到的问题,我需要包含数组的类型作为模板.
Note that removing the template is not an option, i made this example to reproduce a problem that i'm having in a code where i need the type containing the array to be a template.
谢谢
推荐答案
http://ideone.com/3ssVi
它用 G++ 编译得很好.
it compiles fine with G++.
据我所知,它可能与此错误有关:
As far as i can see it can be related to this bug:
http://connect.microsoft.com/VisualStudio/feedback/details/759407/can-not-get-size-of-static-array-defined-in-class-template
这篇关于C2070 - 操作数大小非法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!