7

As far as I can see in the standard, the following code is valid. It compiles in MSVC1025.

const struct omg;
struct omg volatile;

int main()
{
    return 0;
}

The qualifiers const and volatile seem purposeless in those declarations. They do not help nor hurt neither the compiler nor the programmer.

The standard does not seem bent on weeding out these "empty ambiguities". In the case of the empty declaration ;, it is explicitly allowed.

Are there other cases of tokens that, after preprocessing, are irrelevant for the meaning of the expression?

Hector
  • 2,464
  • 3
  • 19
  • 34
  • 1
    [`register`](http://stackoverflow.com/questions/3207018/register-keyword-in-c) – m.s. Oct 21 '15 at 15:44
  • 8
    I would not consider MSVC a reliable arbiter of what the standard allows or requires. And I'm inclined to doubt that the situation will have changed by year 11025 :-) – John Bollinger Oct 21 '15 at 15:48
  • 2
    fails on g++: http://coliru.stacked-crooked.com/a/b486009ecb10f5e7 – NathanOliver Oct 21 '15 at 15:51
  • Where in the standard can I find something equivalent to "qualifiers can only be specified for objects and functions? That is the g++ error message. – Hector Oct 21 '15 at 16:10
  • *"after preprocessing"* does not mean what your question seems to imply. After preprocessing means: when the preprocessor has finished. – Christian Hackl Oct 21 '15 at 16:30
  • @ChristianHackl: I did mean after the preprocessor has finished. I meant to avoid empty macros. – Hector Oct 21 '15 at 16:31

1 Answers1

4

Both clang and gcc reject this code using -pedantic-errors. clang provides the following error:

error: 'const' is not permitted on a declaration of a type [-Werror,-Wmissing-declarations]
const struct omg;
^

error: 'volatile' is not permitted on a declaration of a type [-Werror,-Wmissing-declarations]

the draft C++ standard section 7.1.6.1 The cv-qualifiers [dcl.type.cv] says:

[...]If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list of the declaration shall not be empty.[...]

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • I upvoted and accepted your answer even though it did not answer my question as it was worded. I was looking for irrelevancies such as `if(true);` that are neither full expressions nor depend on other declarations/expressions. – Hector Oct 21 '15 at 16:36
  • @Hector I completely missed that aspect of your question, I read in a more narrow sense. It is possible you will receive a broader answer, that would not be unusual. You should feel free to unaccept in the mean-time, questions without an accept usually get more views and sometimes more answers and I would completely understand. – Shafik Yaghmour Oct 21 '15 at 16:44
  • @Hector in general the standard eschews ambiguity and either makes such cases ill-formed or undefined behavior. In other cases the standard will define how ambiguities should be resolved. This makes sense since one of the goals of a standard is to remove potential ambiguities and provide uniform implementations. – Shafik Yaghmour Oct 21 '15 at 16:51