Topic: Constant member functions


Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/02/21
Raw View
In article <6cf7sc$s30@engnews1.Eng.Sun.COM>, clamage@Eng.sun.com
says...

[ ... ]

> (Come to think of it, it might be useful to be able to say that a
> function doesn't modify *any* globals; it could introduce
> opportunities for optimization.)

...and (IMO more importantly) documentation for other programmers
reading it.  However, this raises the question of what you do to a
function that promises not to modify any globals, but does (perhaps
indirectly) anyway.  Do you simply declare that it's undefined
behavior, or you you try to diagnose it at runtime, or perhaps
something else.

Somehow this reminds me a bit of the situation with a function that
throws an exception it's promised not to in its exception declaration.
Certainly exception declarations seem like a good idea offhand, and
should be extremely useful, but sometimes the real results aren't
particularly desirable -- to the point that some people refuse to use
exception declarations at all.

--
    Later,
    Jerry.

The Universe is a figment of its own imagination.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@Eng.sun.com (Steve Clamage)
Date: 1998/02/18
Raw View
In article AA07415@LL.MIT.EDU, David Cogen <cogen@ll.mit.edu> writes:
>> Why would you want to do it?  What would it mean?
>>
>> Saying `const' after the argument list of a member function means
>> that the function will not change the object to which `this' points.
>> But a static member function has no `this' pointer.
>
>By simple analogy, saying const to a static member function would mean that
>the function will not change any of the class's static members.
>
>This seems completely intuitive, and useful, and I am disappointed that this
>is not part of the language.
>
>But I suppose, const non-static member functions should be prohibited from
>altering static data too, but I don't think this is enforced, is it?

When a parameter is declared as pointer-to-const, it means the particular
object to which it points will not be modified by the function. A
non-static member function has an implied pointer to one particular
object of its type. If the "this" pointer were explicit, we could write
 void T::f(const T* this, ...);
but we can't. The only available place to put the const, barring
introducing more keywords or even stranger syntax, was after the
function header:
 void T::f(...) const;

The exact meaning is that the (one particular) object to which "this"
points will not be modified (except for mutable members). It says
nothing about other T objects. Example:
 void T::f(T* other) const { other->memb = 0; } // OK

Similarly, the const says nothing about static class members, since
they are separate objects themselves, not part of any T object.

The reason for const member functions is so you can create const
objects of the class type (or for a function to promise it won't
modify an object passed by pointer or reference) and still have a
way to call member functions on such objects.

If you want a static data member (and there is only one in the entire
program) to be const, declare it const. But this proposal says
that a particular function should be able to promise not to
modify a particular object which is not one of its parameters.

If we could specify that a static member function would not modify
static members, shouldn't we also have a way to specify that non-static
member functions would not modify static members? For that matter,
shouldn't you also be able to specify that a non-member function
(e.g. a friend function that is part of the class implementation)
would not modify any static members?

We also don't have any way to declare, for example, that global function
g doesn't modify global variable x. Isn't that a comparable situation?

(Come to think of it, it might be useful to be able to say that a
function doesn't modify *any* globals; it could introduce
opportunities for optimization.)

Personally, I don't see any utility in being able to declare (only)
that a static member function doesn't modify any static data members
of its class.

---
Steve Clamage, stephen.clamage@sun.com




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David Cogen <cogen@ll.mit.edu>
Date: 1998/02/18
Raw View
> Why would you want to do it?  What would it mean?
>
> Saying `const' after the argument list of a member function means
> that the function will not change the object to which `this' points.
> But a static member function has no `this' pointer.

By simple analogy, saying const to a static member function would mean that
the function will not change any of the class's static members.

This seems completely intuitive, and useful, and I am disappointed that this
is not part of the language.

But I suppose, const non-static member functions should be prohibited from
altering static data too, but I don't think this is enforced, is it?

-- David Cogen


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paulo Abreu" <pama@mail.telepac.pt>
Date: 1998/02/17
Raw View
Why am I allowed to do:

static void X::function (const X&, const &X) {//     }

and I can't do that:

static void X::function( X&, X&) const {/. }

Any help would be appreciated

----------------------------------------------------------------------------
------------------------------
// this program doesn't cause any errour
#include <iostream.h>

class X{
 int a, b;
public:
   X() { a=b=0; }
 static void function (const X& a, const X& b) {cout << a.a; cout << b.a;}
};

//this program will cause a compiler errour
#include <iostream.h>

class X{
 int a, b;
public:
   X() { a=b=0; }
 static void function (X& a, X& b) const {cout << a.a; cout << b.a;}
};
----------------------------------------------------------------------------
--------------------

Paulo Abreu

PS: Reply for author, if possible.


Why am I allowed to do:

static void X::function (const X&, const &X) {//     }

and I can't do that:

static void X::function( X&, X&) const {/. }

Any help would be appreciated

----------------------------------------------------------------------------
------------------------------
// this program doesn't cause any errour
#include <iostream.h>

class X{
 int a, b;
public:
   X() { a=b=0; }
 static void function (const X& a, const X& b) {cout << a.a; cout << b.a;}
};

//this program will cause a compiler errour
#include <iostream.h>

class X{
 int a, b;
public:
   X() { a=b=0; }
 static void function (X& a, X& b) const {cout << a.a; cout << b.a;}
};
----------------------------------------------------------------------------
--------------------

Paulo Abreu

PS: Reply for author, if possible.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Paul Black <paul.black@vf.vodafone.co.uk>
Date: 1998/02/17
Raw View
"Paulo Abreu" <pama@mail.telepac.pt> wrote:
 > Why am I allowed to do:
 >
 > static void X::function (const X&, const &X) {//  }
 >
 > and I can't do that:
 >
 > static void X::function( X&, X&) const {/. }
 >
 > Any help would be appreciated

const is used for methods that are invoked on const
objects. What object is X::function invoked on?
(Hint: what is the syntax for calling a static
function?).

Paul
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]