I am wondering about how C++ uses its const keyword.
I have the following function definition. Which alone looks quite insane, but works just fine.
const int const * const Get(){ return new int(1); } const
I am aware of what each placement of the const means, this question isn't about the meaning of the placement of the const keyword.
I am quite confused by the use of the const keywords, because you can duplicate them.
const int const const * const Get(){ return new int(1); } const
// or even
const const int const const * const const Get(){ return new int(1); } const const
// or even yet
const const const int const const const * const const const Get(){ return new int(1); } const const const
Why does the language allow you to do this?
EDIT: This code can be compiled in Visual Studio 2013, Visual C++ compiler. I am not sure about the actual name of the compiler.
EDIT2:
So the answer is that this is against the standard. The code only compiles wihout using /Za
option.
I am voting to close the question.
Why? Because the standard says so. Here is an excerpt from [dcl.type.cv] which states exactly this (emphasis mine) :
This makes sense in templates, for example. If the template parameter is deduced as
const
, it can easily happen that another const is added somewehere.EDIT: as noted several times and redundantly, my above answer is misleading in that it does not qualify here. It is opposed by the rule in [dcl.type] which explicitly disallows explicitly typed
const
qualifiers (see the fine comment by @TartanLlama in his answer).EDIT 2: the application of the rule everyone seems to agree on states: first redundant
const
s are disallowed, and if they still should somewhere occur they are ignored.This, however, requires a priority of the standard quotes.
Without, one could also think of an order like: first remove redundant
const
s, and only then apply the rule that multipleconst
s are not allowed (which, of course, would render the latter rule itself redundant).In this case, obviously, the quote shows how it is meant to be interpreted. But, being pedantic, it does not have to be interpreted like this -- unless there is some form of priority in the standard quotes.