Path: utzoo!attcan!uunet!mcsun!ukc!edcastle!dcl-cs!aber-cs!pcg From: pcg@aber-cs.UUCP (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: [SUMMARY] Three ways to declare a function "const" in C++ 2.0 Message-ID: <2057@aber-cs.UUCP> Date: 19 Oct 90 14:52:07 GMT Reply-To: pcg@cs.aber.ac.uk (Piercarlo Grandi) Distribution: comp Organization: Dept of CS, UCW Aberystwyth (Disclaimer: my statements are purely personal) Lines: 45 In article <4454@husc6.harvard.edu> ngo@tammy.harvard.EDU (Thomas Ngo) writes: In article , 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