One Book(Object Oriented Programming with C++ by E.Balagurusamy) says that
const size = 10;
means
const int size = 10;
but g++ compiler (version-4.6.1 in ubuntu) issues an error as
error: ‘size’ does not name a type
what should I conclude based on this?
- g++ doesn’t support the feature.
- It is new feature. Latest g++ version supports it.
- The statement is wrong. Data-type is mandatory with the const keyword.
- Something else.
Looks like an error in the book … you definitely must name a type or aliased type (i.e., a
typedef) since C++ is a strongly-typed languages.Here is what the C++03 specification states on objects, declarations and definitions:
Section 1.8/1:
Then in Section 3.1/1:
Then in Section 3.1/6:
Finally, in Section 3.9.2/1 it states:
So according to 3.9.2/1,
constis a qualifier, not a type, and as-such, it has to qualify a valid unqualified type. Secondly, in the example given, according to 3.1/1, the declaration ofsizeis also a definition, and therefore thesizeobject must have an associated type or the program is ill-formed according to 3.1/6.