ngo@tammy.harvard.edu (Tom Ngo) (10/16/90)
Thanks to all those who replied to my (badly worded) query, posted in comp.lang.c++: ngo> In g++, you can declare a function "const", e.g. ngo> const double sin( double x ) ngo> if the function examines nothing but its arguments and has no ngo> side-effects. (Such a function can be subject to common ngo> subexpression elimination.) Is this a part of C++ 2.0? If not, ngo> why not? I say my query was badly worded because most of the replies (understandably) described "const member functions", i.e. member functions which (unless const is casted away) do not modify *this. The syntax for defining such a function would go: double sin( double x ) const { /* ... */ } My question really referred to the syntax in my original posting, i.e. const double sin( double x ) { /* ... */ } AT&T and GNU C++ treat this syntax in different ways: [AT&T] The "const" modifies the type "double", so the function returns a "const double", i.e. a double which cannot be modified. In this particular context, the "const" is meaningless--the value returned by a function to its caller can't be modified anyway. [GNU] The "const" syntax is co-opted to mean something else. It now declares something about the function itself: that if the function is called twice, and the arguments are not altered between calls, the second call to the function can be eliminated without changing the meaning of the program. This kind of const is used to eliminate redundant calculations in a sequence like double x, y, z; x = M_PI * 0.25; y = sin(x); z = sin(x); The compiler can replace this with double x, y, z; x = M_PI * 0.25; y = sin(x); z = y; My opinion, for what it's worth, is that the G++ meaning is much more useful. Tony Hansen remarked: It's a relatively new extension of g++. I'm sure that it will be considered by the ANSI committee. I'm also sure that Bjarne Stroustrup has looked at the extensions that g++ provides, but I don't think any of them have made it back into C++ in the same fashion that g++ implemented them. -- Tom Ngo ngo@harvard.harvard.edu 617/495-1768 lab number, leave message
scp@acl.lanl.gov (Stephen C. Pope) (10/17/90)
on 16 Oct 90 14:26:25 GMT, ngo@tammy.harvard.edu (Tom Ngo) said: [...] ngo> In g++, you can declare a function "const", e.g. ngo> const double sin( double x ) [...] Tom> [GNU] The "const" syntax is co-opted to mean something else. It Tom> now declares something about the function itself: that if the Tom> function is called twice, and the arguments are not altered Tom> between calls, the second call to the function can be eliminated Tom> without changing the meaning of the program. This kind of const Tom> is used to eliminate redundant calculations in a sequence like [...] Tom> Tony Hansen remarked: Tom> It's a relatively new extension of g++. I'm sure that it will be Tom> considered by the ANSI committee. I'm also sure that Bjarne Tom> Stroustrup has looked at the extensions that g++ provides, but I Tom> don't think any of them have made it back into C++ in the same Tom> fashion that g++ implemented them. Only thing is, g++ doesn't always do anything with it; this is especially true of virtual methods. Word is that this g++ extension is going away...! stephen pope advanced computing lab, lanl scp@acl.lanl.gov
ngo@ymer.harvard.EDU (Thomas Ngo) (10/18/90)
In article <SCP.90Oct17100405@grizzly.acl.lanl.gov>,
scp@acl.lanl.gov (Stephen C. Pope) replied to my original posting:
scp> ngo@tammy.harvard.edu (Tom Ngo) said:
scp> ngo>
scp> ngo> In g++, you can declare a function "const", e.g.
scp> ngo> const double sin( double x )
scp> ngo> [indicating that successive calls to this sin() need not be
scp> ngo> repeated if x has not changed in the interim...]
scp> ngo>
scp> ngo> [In AT&T C++ 2.0, the "const" means the function returns a
scp> ngo> "const double", which is meaningless in this particular context...]
scp> ngo>
scp> ngo> Tony Hansen remarked:
scp> ngo> It's a relatively new extension of g++.
scp>
scp> Only thing is, g++ doesn't always do anything with it; this is especially
scp> true of virtual methods. Word is that this g++ extension is going away...!
Interesting. In support of making this extension go away, someone
else pointed out to me that in a case like
const double* foo()
the const would NOT be meaningless in AT&T C++ 2.0.
Tom Ngo
ngo@harvard.harvard.edu
617/495-1768 lab number, leave message
pcg@aber-cs.UUCP (Piercarlo Grandi) (10/19/90)
In article <4454@husc6.harvard.edu> ngo@tammy.harvard.EDU (Thomas Ngo) writes: In article <SCP.90Oct17100405@grizzly.acl.lanl.gov>, scp@acl.lanl.gov (Stephen C. Pope) replied to my original posting: ngo@tammy.harvard.edu (Tom Ngo) said: ngo> In g++, you can declare a function "const", e.g. ngo> const double sin( double x ) ngo> [indicating that successive calls to this sin() need not be ngo> repeated if x has not changed in the interim...] This is a lame description of the effect. The constness of a function result is ignored by standard C++ compilers, but to G++ irt means that the return value of the function only depends on the arguments to it, and that it does not have relevant side effects. G++ is then free to avoid reissuing a function call if the result of a call with the same parameters has been saved. scp> Only thing is, g++ doesn't always do anything with it; this is scp> especially true of virtual methods. Word is that this g++ extension scp> is going away...! Too bad. It is great nbot just to allow the compiler to implement caching of function results, but also to document the fact that the programmer believes the function to be reducible. Since it is usually very interesting to know which functions are reducible and which are stateful, the fading away of this use of const is deprecable because it gives one incentive less to the programmer to document assumptions. Interesting. In support of making this extension go away, someone else pointed out to me that in a case like const double* foo() the const would NOT be meaningless in AT&T C++ 2.0. Of course. But equally of course this would have a specific meaning to GNU C++ (the returned value only depends on the arguments) and not to standard C++, and indeed the following line is quite different from the preceding one: double *const foo() -- Piercarlo "Peter" Grandi | ARPA: pcg%uk.ac.aber.cs@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcsun!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk