views:

175

answers:

3

Is the following legal C++ code:

class C 
{
     static public  int x;
};

It compiles OK in Visual Studio 2008 C++ and Visual Studio 2010 C++ (beta 2). But the static member x does not end up being public.

In Visual Studio 2010 beta 2 the experience is even stranger. Intellisense reports an error "expected an identifier", but the compiler does not. Visual Studio 2008 does not give any error.

So the questions are:

Is this legal C++ code? What does it mean?

+6  A: 

This is not legal C++. It is a legal C#, so that's why MS IDE bugged out.

Correct:

public: static int x;

Pavel Radzivilovsky
But it does compile. And if it is not legal, the the IDE did the right thing and the C++ compiler did the wrong thing (atleast in vs.net 2010).
Arve
Well, this is indeed surprising. However, "for each(x in someStlCollection)" also compiles even on MSVC++ 8, and this is totally OFF. It's c C++CLI syntax that somehow drifted into C++ compiler by means of diffusion. C++, by the way, provides a foreach syntax of it's own, starting C++0x.
Pavel Radzivilovsky
+2  A: 

No it is not legal C++

It may be legal C# (but you would need to check with a C# person).

Martin York
+4  A: 

It's not legal C++ code.

The 'public' isn't allowed in variable declarations. What you're seeing however is that the compiler 'works' because it also compiles as CLI (.NET code) and there it's allowed and legal.

Stefan