As you know, links cannot be reassigned, since they always refer to the same object and, therefore, are always constant. However, the C ++ syntax allows for the existence of permalinks:

int var = 33; int const & r1 = var; // ссылка на константу int & const r2 = var; // постоянная ссылка 

r1

In the case of references to constants, everything is quite obvious. If the argument is passed to the function " by reference ", then the const modifier can be used to protect the value from accidental changes in the process of writing program code.

r2

But what is the point of explicitly indicating the constancy of a link?
And where it can be applied in practice is not clear to me.

    2 answers 2

    GCC (gcc-5.1) on ideone does not compile your code:

     prog.cpp: In function 'int main()': prog.cpp:8:17: error: 'const' qualifiers cannot be applied to 'int&' int & const r2 = var; // постоянная ссылка 

    Visual Studio 2015 when compiling issues a warning:

    test.cpp (11): warning C4227: anachronism used: qualifiers on reference are ignored


    The current standard, 8.3.2 / 1 (by the way, a link to it here ), says the following about it:

    The typedef-name ([dcl.typedef], [temp.param]) or decltype-specifier ([dcl.type.simple]) , in which case the cv-qualifiers are ignored.

    Thus, const , referring to a link, is either invalid or ignored.

    • I like the GCC approach more, since despite the fact that the syntax of the language allows such a construction, from the point of view of common sense, this statement is also ambiguous, like “oil of oil”. In Visual Studio, I also checked, const is simply ignored, with warning C422 indicated. VladD, thanks for the prompt and accurate answer on the topic! - Michael
    • @Michael: In addition, behavior like GCC is required by the standard. I think MSVC was done for backward compatibility. - VladD
    • @Michael: Please! - VladD

    Your question is as if built on the implicit assumption that the admissibility of such a construction in terms of the grammar of a language in some way implies its “necessity” or “meaningfulness”.

    This is a wrong assumption.

    A competent design of the language just requires the choice of a reasonable distribution of areas of responsibility between the formal syntax (ie, grammar) and additional rules, described separately, "in words." Attempts to implement all sorts of such restrictions entirely at the level of the grammar of the language would have led to incredibly overcomplicated grammar. In this case, obviously, it was decided not to try to implement this restriction at the grammar level.

    The specification of C and C ++ languages ​​contains a huge number of examples of such situations where restrictions that could be attributed to purely syntactic ones (perhaps with a certain stretch) are nevertheless implemented not at the level of the grammar of the language, but as separately described restrictions.

    For example, in the C language (and, until relatively recently, in C ++) it was forbidden to use the comma operator in constant expressions. However, despite the fact that the grammars of these languages ​​have separate non-terminal characters for the "constant expression", this prohibition was implemented at the grammar level only for the top-level commas. A ban on the use of deeper commas (inside parentheses) in the grammar is not reflected, but formulated in the language specification "in words", because it would be too wasteful to build a completely independent sub-grammar of constant expressions.

    • Well, this assumption is usually quite reasonable, a good language should minimize the possibilities for meaningless constructions. Example: ideone.com/llaR5n - VladD
    • On account of the limitations that lead to complication - this is very controversial. It always seemed to me the opposite. For example, in Python, expressions do not end with a semicolon, this limitation at the syntax level allows you to avoid making obscenities of errors, in the form of missing or, to the contrary, redundant characters ";". There are plenty of other examples, but the comment format does not allow to bring them all. - Michael
    • @Michael, only in python I had to add the pass operator - avp
    • @Michael: In C ++, expressions don't end either ; . In C ++ ; - this is the final character of some type of statements , and even then not all. - AnT
    • one
      @Michael: The reason for my use of the English term statement is precisely that it has not yet come up with an adequate translation into Russian. In the literature, it is usually translated as an operator , which gives rise to an obvious (and, in my opinion, unacceptable) conflict with the term operator (which is also translated as an operator ). Therefore, despite the fact that I use the Russian terms in the Russian text with both hands, in order to avoid interpreting ambiguity in sensitive contests, I sometimes prefer to explicitly use the untranslated statement . - AnT