I am currently porting a program developed on Linux to Win32. Amongst other problems, I have one that is pretty weird.
A header file contains something like this:
namespace Networking {
struct MetaStruct
{
int iDataType;
int iDataSize;
void* pData;
};
const int MetaStructSize = sizeof(MetaStruct) - sizeof(MetaStruct::pData);
};
This compiles fine on fine on Linux, but I get this error when compiling for Win32 using VS2010:
Networking.hpp(50): error C2070: '': illegal sizeof operand
I tried adding the Networking:: before MetaStruct but it doesn’t change anything. The weird thing is VS2010 gives me the correct value of the sizeof when I hover it with the mouse, but won’t compile it. Why?
In C++03 There are two forms of sizeof expressions (see ISO/IEC 14882:2003 5.3.3 [expr.sizeof]).
MetaStruct::pDatais neither a valid expression (resolving to an object type) nor the name of a type.You would have to do
or
Update: Thanks to @hvd who points out that this should actually be legal in C++11 now.
You can now use an id-expression that refers to a non-static member of a class in contexts where it isn’t evaluated. Evidently this isn’t supported by VS2010.
There’s also a new form of
sizeofin C++11:sizeof ... ( identifier )but that’s not relevant here.