Here is (one of) the dictionary definitions of "constant":
a situation or state of affairs that does not change.
Pretty much exactly what I said it would be...
That definition applies to a compiler constant just as it does to anything else. (well, in all fairness, Pascal has writable constants so, there is at least one additional problem associated with Pascal's creative definition of what is a constant.) IOW, the definition in the dictionary is definitely something the compiler is _expected_ to abide by (just as anything else that claims that something is constant.)
It would be really nice if the compiler honored the meaning of the words it uses, "const" among them.
And that also defines very much the information
"I, the uses, will not change that, therefore it is constant. While you, the compiler, can not check for that, please believe me and use the information as you are pleased"That says exactly: The value is constant.
And it says to the compiler: Abide or conform to that. I tell you, you conform with it.
The current implementation is exactly as the dictionary implies.Please, take the exact one sentence you said was from the dictionary, and explain how that sentence says anything about "enforced by the compiler", and can according to you not mean "enforced by the user"
=> actually that sentence does not even say anything about enforcing.
=> that sentence just states the result "does not change"
and when the user does ensure it does not change it, then that result is achieved.
So how do you get anything else out of that dictionary definition?
As far as the rest goes...
In the first few replies in this thread, there is an _implication_ that the use of "const" would produce better code, IOW, it would allow the compiler to perform optimizations it cannot do otherwise.
Yes, they did. And yes that is true. That is what the const modifier does.
Give the compiler the additional knowledge that the value will be const. So the compiler can use (conform/abide to) that knowledge.
But, those replies did not (as far as I can see) say, that this optimization is expected to be passing by ref with the indent to conform to some "by ref" calling convention.
There is a difference between calling by ref, as a functional change (the callee actually makes needs a ref). And using a ref as optimization, without any change to behaviour. The latter may be calling by ref, but the
expectation is not about the ref, but about the performance.
The point I made in my first post and, that I maintain, is that "const" should NOT be used to cause the compiler to perform some optimization.
Why?We have other hints to allow optimizations, e.g. inline.
It does not conflict with the dictionary meaning of the word "const".
Since it does not trigger a specific optimization, but rather allows generally for various optimizations, the keyword can not be based on the optimization itself.
Therefore the keyword simple states the fact (this value is const / I the user have made sure of it), and then leaves the compiler the freedom to deal with it.
Just like the compiler can replace a multiplication with a counter, by an addition of the step value. I get the result that the multiplication would give me. But I don't actually get the code that does the multiplication.
"const" should be used to document how an item is used, nothing else.
Interesting.
Because, I thought you wanted "const" to tell the compiler to check that this use case is obeyed? But that would be "something else".
If there truly is "nothing else", then why even bother telling the compiler.
If there is, then the word "const" as in the dictionary, does not disqualify either choice. So out of the 2 possible "something else", the current one is a valid choice.
IF the programmer wishes to have the compiler "optimize" how the parameter is passed, which normally means by reference, then the programmer should specify "constref"
That is wrong.
Optimize does not "normally mean by reference".
That is just one of many optimization that may or may not be applied.
And a lot of times people use const for the "omit managed type refcount" optimization.
There a plenty of cases where users use "const" especially for ansistring.
- Passing an ansistring by reference (reference to the internal pointer / aka truly double reference) is not what is wanted then
- not doing the InterLockedIncrement is what is wanted
So, no: const is not "normally" used as a means to want "by reference"And it isn't in this thread here, since this was about ansistrings.
1. the documentation _specifically_ states that "const" should NOT be expected to pass a parameter by reference.
Yes, I agreed.
And, I have not seen anyone expecting this.
Please show where someone expects this.3. I've seen _plenty_ of FPC code that _incorrectly_ assumes using "const" causes passing by reference (and unfortunately in those cases it does)
"Plenty" ... How is that defined? More than how many percent of the uses of "const param"?
Also, there is a difference between individual uses of a feature and the feature itself. If there are some incorrect uses, then thats a problem for them, not for the feature.
And well, yes, next is "but if the feature invites it" => lets also get rid of asm, pointer, utf8, .... All of them invite mistakes (by some definition, that can be made up).
As far as point 3 above, an example of that is the passing of IIDs and specifically the incorrect definition of REFIID as NOT being a pointer to a GUID. Instead the definition pretends a REFIID is a GUID and _wrongly_ depends on "const" (instead of "constref") to cause the compiler to pass by reference.
Ok, I don't know where that code is.
But, then (if you are right) that would be ONE wrong use. And maybe there are some other.
The majority of use cases that I have seen for "const" param, are not wrong (in the sense that they should be constref).
In fact, the majority of uses of "const param" would be wrong, if they were changed to constref. Because many of them want to be "pass by value", but rely on other optimizations done by "const"
E.g. any use of "const" with "ansistring", or dyn-array, or open-array => all of those definitely do not want "pass by ref" And those are plenty.
1. I don't have anything against "const".
2. I have a problem with "const" being _misused_ to trigger one or more compiler optimizations. That's NOT what "const" is for.
3. if a const should or needs to be passed by reference, there is a modifier for that: "constref", use it, that's what it's there for.
(2) is exactly what it is for. That is
- documented
- does not conflict with the dictionary
(3) I have agreed on the "need" part several times.
"should" is ambiguous as it can include "happens as a side effect". And side effects, that do not alter the behaviour of the code, well they are everywhere, so I don't see why this one would be special. (as long as it is not mixed with the "functional required" feature).