Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is type specifier required for const?

Tags:

c++

constants

Is a type specifier required here?

const c = 7;

Bjarne Stroustrup's 'The C++ Programming Language' on page 80 says that this is illegal. However, I've been practicing some brainbench tests, and one of the questions states that the type defaults to int. Brainbench is usually correct, so I'm unsure of which reference is right, and I've been unable to find anything in the standard. Does anyone have a definitive answer and a reference?

like image 787
Trent Avatar asked Dec 02 '22 06:12

Trent


2 Answers

The default type of int is valid for C, but not for C++. Even in C this style of coding should be avoided. Also note that Bjarne Stroustrup's book is one of the most authoritative reference for standard C++.

like image 128
Vijay Mathew Avatar answered Dec 09 '22 10:12

Vijay Mathew


For C++ I would believe Stroustrup over any place but a standard.

Perhaps the question was about C not C++?

The draft C++0x standard in section 7.1.6 says

At least one type-specifier that is not a cv-qualifier is required in a declaration unless it declares a constructor, destructor or conversion function.83 A type-specifier-seq shall not define a class or enumeration unless it appears in the type-id of an alias-declaration (7.1.3).

like image 21
mmmmmm Avatar answered Dec 09 '22 10:12

mmmmmm