Topic: Continue: static func() const; legal?


Author: Dick Menninger <Dick.Menninger@DaytonOH.ATTGIS.COM>
Date: 1995/11/07
Raw View
I tried to participate in this general thread
earlier but my news server had severe case
of out of date newsgroup tables and mailed
it (and other replies) to oblivion.  So this is
slightly out of context.

When a normal (i.e., non-static) member function
is given a const qualifier, it specifies that the
function does not modifies normal members of
the object instance.  But such a member function
can diddle with static members of the class without
degrading its const status (at least that is what
I remember).  So, if you want a means to say
NO SIDE-EFFECTS WHATSOEVER, it should
be done in a way that can say that for normal
member functions, as well.  The debate should be
about whether that is a good thing to be able to say
about functions.  If it is, then you want to be
able to say it is ALL contexts and that changes
how you wind up saying it.

Good Day
Dick
Dick.Menninger@DaytonOH.ATTGIS.COM


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: Bret Pehrson <BretP@strata3d.com>
Date: 1995/11/01
Raw View
A while back I asked whether the following was legal:

class base
{
  static funkie(void) const;
};

I received two responses, both of which said (paraphrased):

It is not legal, and does not make sense because 'const' implies that
the this pointer is constant, and since 'static' members do not have
a this pointer, it is meaningless.

After some thought, I would like to repose the question:

Suppose:

class base
{
  static void lumpy(void) const;
  static int cLumps;
};
int base::cLumps = 0;


void base::lumpy(void)
{
  ++cLumps;
}


Now, since cLumps is a member of base, albeit static, making a static
member function 'const' would mean that it does not change available
class members (in this case, static member variables).

So, with this above sample, I can definitely see a legitimate need
for static const member functions.

Legal or not?



[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]






Author: ralphs@tesser.com (Ralph Shnelvar)
Date: 1995/11/02
Raw View
matt@cyclops5.berkeley.edu (Matt Austern) wrote:

>In article <478k57$5gl@stellar.comnet.com> Bret Pehrson
><BretP@strata3d.com> writes:
>
>There's no way to declare a function such that it's prohibited from
>modifying global variables; similarly, there's no way to declare a
>function such that it's prohibited from modifying static member
>variables.

That's a double pity.

First, it is a pity that one cannot declare a function and say "You
can't access global variables.

Second, it is a pity that class static variables cannot be defined as
untouchable.

Thanks for pointing this counter-intuitive use of
 function() const
out.  I actually had to run a test compilation to believe you.

Ralph
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: "linh (l.) dang" <linhd@bnr.ca>
Date: 1995/11/03
Raw View
Ralph Shnelvar (ralphs@tesser.com) wrote:
: matt@cyclops5.berkeley.edu (Matt Austern) wrote:

: >In article <478k57$5gl@stellar.comnet.com> Bret Pehrson
: ><BretP@strata3d.com> writes:
: >
: >There's no way to declare a function such that it's prohibited from
: >modifying global variables; similarly, there's no way to declare a

Using GCC-xtensions one can write:

void foo() __attribute__ ((const));

foo() then is a function without side-effects. Since class static
variables is a subset of global-variables, a const static member
functions would have no side-effects on this subset.

------------------
L.D.

: >function such that it's prohibited from modifying static member
: >variables.

: That's a double pity.

: First, it is a pity that one cannot declare a function and say "You
: can't access global variables.

: Second, it is a pity that class static variables cannot be defined as
: untouchable.

: Thanks for pointing this counter-intuitive use of
:  function() const
: out.  I actually had to run a test compilation to believe you.

: Ralph
: ---
: [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
:   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
:   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]