www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Const function

reply Gilles G. <schaouette free.fr> writes:
(... start a new thread because I just don't know who to reply to)

Many posts about const/invariant talk about the "problem" of defining const
functions. Some think we should indicate constness at the end of the function
declaration, but it is also possible to do it at the front.
So, as far as I understand it, 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?
Nov 28 2007
next sibling parent reply Graham St Jack <Graham.StJack internode.on.net> writes:
On Thu, 29 Nov 2007 02:16:40 -0500, Gilles G. wrote:

 (... start a new thread because I just don't know who to reply to)
 
 Many posts about const/invariant talk about the "problem" of defining
 const functions. Some think we should indicate constness at the end of
 the function declaration, but it is also possible to do it at the front.
 So, as far as I understand it, 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();
Dec 02 2007
parent Gilles G. <schaouette free.fr> writes:
I see I am not the only who don't understand why const function written like
this:
    T const myFunction()
was not envisaged.
Could anyone explain why?
Thanks in advance!

--
Gilles


Graham St Jack Wrote:

 On Thu, 29 Nov 2007 02:16:40 -0500, Gilles G. wrote:
 
 (... start a new thread because I just don't know who to reply to)
 
 Many posts about const/invariant talk about the "problem" of defining
 const functions. Some think we should indicate constness at the end of
 the function declaration, but it is also possible to do it at the front.
 So, as far as I understand it, 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();
Dec 03 2007
prev sibling parent reply Jason House <jason.james.house gmail.com> writes:
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.
Dec 04 2007
parent Jesse Phillips <jessekphillips gmail.com> writes:
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.
Dec 04 2007