VS14 CTP – Extended sizeof

The Visual Studio 14 CTP is now available as a VM on Azure. The C++ compiler in the CTP has several enhancements for C++ 11 and C++ 14 features. C++ 11 proposes a feature to extend sizeof to apply to non-static data members without needing a temporary object. The CTP implements that feature.

Consider the following struct.

struct S
  int data;
  char data2;

Now with Visual Studio 2013, the following code will not compile.

cout << sizeof(S::data) << endl;

You’ll get this error message.

Error 1 error C2070: 'unknown': illegal sizeof operand

You’ll need to do this instead.

S s;
cout << sizeof(s.data) << endl;
cout << sizeof(s.data2) << endl;

Compare this to the following code supported in the CTP.

cout << sizeof(S::data) << endl;
cout << sizeof(S::data2) << endl;

It’s a simple feature but it will help you write cleaner code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s