Const function

Jesse Phillips jessekphillips at gmail.com
Tue Dec 4 14:01:09 PST 2007


On Tue, 04 Dec 2007 13:48:10 -0500, Jason House wrote:

> Graham St Jack Wrote:
> 
>> On Thu, 29 Nov 2007 02:16:40 -0500, Gilles G. wrote:
>> 
>> > there are two ways to express function constness for now:
>> >    const int foo();
>> >    int foo() const;
>> > To my mind, both solutions are unintuitive. I would expect something
>> > like that:
>> >    int const foo();
>> > Is there any big argument against this?
>> 
>> I agree. A definition like:
>> 
>> const T foo();
>> 
>> looks to me like the returned T is const, and putting the const after
>> the function is way too non-D for me, so all that is left that makes
>> sense is:
>> 
>> T const foo();
> 
> I've seen Walter argue that he wants to be able to declare const
> functions in batch with const{
>   T foo();
>   T bar();
> }
> 
> I guess you could say that he wants const{X;} and const X; to be the
> same, but doesn't want const(X) and const X to be the same.  There's
> nothing like mixing two hotly debated threads together!  I apologize in
> advance for it.  I just couldn't resist since the parallels were so
> striking.

I would have to argue that const {W;} and const X; are the same while 
const W* var; and const(W*) var; are different. As described in "Teach 
Yourself C++ in 21 Days" a block/compound statement is used to act as one 
statement. That is to say that only single statements are acceptable. 
That is to say that {W;} is in fact only X.

Now look at the second case, const must be applied to something after it, 
Janice assumes that it would stop at the *. However if we continue and 
let W* var = X then we get const(X); with the latter being const(X) var;

My question is why does const X var; have to equal const(X) var? Wouldn't 
it be legitimate to change ones thinking such that const X var is const(X 
var) even thought it is not valid syntax.

I still have a lot to learn and to apply a lot of what I know, but after 
reading posts and reading to understand const and its applications, I see 
the current syntax to work well, even though I don't think it is 
explained very well.



More information about the Digitalmars-d mailing list