Topic: static member function and const


Author: WaterWalk <toolmaster@163.com>
Date: Tue, 13 Nov 2007 10:30:28 CST
Raw View
On Nov 13, 3:43 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class. In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?
>

I think one reason to use const member function is that if the
function is called via a const class object, then there shall be some
way to inform the compiler that "this function does not modify the
object". But a static member function call don't need a object of that
class.  So I don't think there is a need for const static member
function.

If you don't want a static member function modify a static member,
just take some care not to modify. Or you may use a const reference to
that static data member inside you static member function.

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Tue, 13 Nov 2007 16:59:55 GMT
Raw View
subramanian100in@yahoo.com, India wrote:
> why can't a static member function be declared as const ?
> is there a provision to indicate that a static member
 > function does not modify the static data members but only
 > uses their values ?
>
> Kindly explain.

Const member functions are implemented by considering the
this pointer to be a pointer to a const object. Static
methods have no this pointer, and hence can't be const
(or virtual, something else frequently asked for).

It's possible to design other interpretations of what const
should mean, but it's not going to happen for C++.

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: tazmaster@rocketmail.com ("Jim Langston")
Date: Tue, 13 Nov 2007 17:01:54 GMT
Raw View
<subramanian100in@yahoo.com> wrote in message
news:1194932017.215138.261760@e9g2000prf.googlegroups.com...
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class. In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?
>
> Kindly explain.

Didn't you ask this already?  the const for a member function declares that
the hiddent this paramater is constant.  Since a static member function does
not have the hidden this parameter, then the const is meaningless.

That is, if the this was not hidden but had to be declared such as:

void foo::bar( foo* this, int a )
and we wanted to make it constant we would do
void foo::bar( const foo* this, int a )
but since we can't do that, because it's hidden, we put the const at the end
void foo::bar( int a ) const
that const just states that the this pointer is constant.  Since a static
function has no this pointer to make constant, then there is no need to add
const at the end.

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Tue, 13 Nov 2007 22:40:09 GMT
Raw View
subramanian100in@yahoo.com, India ha scritto:
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class. In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?
>

The const on a member function is important because it participates in
overload resolution:

   struct A
   {
     void f(); // #1
     void f() const; // #2
   };

   int main()
   {
     A x;
     x.f(); // calls #1

     const A y;
     y.f(); // calls #2
   }

A const on a static member function won't participate in overload
resolution (because of the absence of the "this" parameter), so it would
practically serve no better purpose than a well written comment.

HTH,

Ganesh

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Roman.Perepelitsa@gmail.com" <Roman.Perepelitsa@gmail.com>
Date: Wed, 14 Nov 2007 13:02:59 CST
Raw View
On 13 Nov, 10:43, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class. In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?
>
> Kindly explain.

It would be pointless, because you can easily remove
const without a cast.

struct foo
{
   static void bar() const;
   int i;
};

int & get_foo_i() { return foo::i; }

void foo::bar() const
{
   int & hacked = get_foo_i();
   // use hacked
}

Regards,
Roman Perepelitsa.

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?iso-8859-1?q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 14 Nov 2007 16:33:36 CST
Raw View
On 14 Nov., 20:02, "Roman.Perepeli...@gmail.com"
<Roman.Perepeli...@gmail.com> wrote:
> It would be pointless, because you can easily remove
> const without a cast.
>
> struct foo
> {
>    static void bar() const;

This is not allowed, but let it remain for a while, because
you are obviously in the process to explain something.

>    int i;
>
> };
>
> int & get_foo_i() { return foo::i; }

This should not compile, but what was your intend?
Was foo::i meant to be a *static* data member?

> void foo::bar() const
> {
>    int & hacked = get_foo_i();
>    // use hacked
> }

If I correctly understand your intend to mean
a static data member i: Yes, your example
shows that current C++ would not have much
advantage of cv-qual. static members
without providing some metaclass concept
and some metaclass members. If these
would exist, those pseudo-static data members
where only available via the corresponding
metaclass (instance). And if done this way,
cv make sense again, because the metaclass
instance itself can be cv-qualified - actually
the counterpart to ordinary object instances
and their cv-qualifications.

Greetings from Bremen,

Daniel Kr   gler


---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Roman.Perepelitsa@gmail.com" <Roman.Perepelitsa@gmail.com>
Date: Thu, 15 Nov 2007 11:06:14 CST
Raw View
Daniel Kr  gler wrote:
> On 14 Nov., 20:02, "Roman.Perepeli...@gmail.com"
> <Roman.Perepeli...@gmail.com> wrote:
> > It would be pointless, because you can easily remove
> > const without a cast.
> >
> > struct foo
> > {
> >    static void bar() const;
>
> This is not allowed, but let it remain for a while, because
> you are obviously in the process to explain something.
>
> >    int i;
> >
> > };
> >
> > int & get_foo_i() { return foo::i; }
>
> This should not compile, but what was your intend?
> Was foo::i meant to be a *static* data member?
>
> > void foo::bar() const
> > {
> >    int & hacked = get_foo_i();
> >    // use hacked
> > }
>
> If I correctly understand your intend to mean
> a static data member i: Yes, your example
> shows that current C++ would not have much
> advantage of cv-qual. static members
> without providing some metaclass concept
> and some metaclass members. If these
> would exist, those pseudo-static data members
> where only available via the corresponding
> metaclass (instance). And if done this way,
> cv make sense again, because the metaclass
> instance itself can be cv-qualified - actually
> the counterpart to ordinary object instances
> and their cv-qualifications.
>
> Greetings from Bremen,
>
> Daniel Kr   gler

Exactly. Thank you for fixing my example
(foo::i was indeed intended to be static) and
clarifying things.

Regards,
Roman Perepelitsa.

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: "subramanian100in@yahoo.com, India" <subramanian100in@yahoo.com>
Date: Tue, 13 Nov 2007 01:43:01 CST
Raw View
why can't a static member function be declared as const ?
We can declare a non-static member function as const,
to indicate that it does not modify the non-mutable data
members of a class. In the same way, is there a provision
to indicate that a static member function does not modify
the static data members but only uses their values ?

Kindly explain.

Thanks
V.Subramanian

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?iso-8859-1?q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Tue, 13 Nov 2007 10:29:27 CST
Raw View
On Nov 13, 8:43 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> why can't a static member function be declared as const ?
> We can declare a non-static member function as const,
> to indicate that it does not modify the non-mutable data
> members of a class.

Slightly imprecise: The const-qualification specifies whether
the member function does potentially modify a given instance
of a class, more specifically a const member function can be
thought of having a hidden this-pointer to the *const* class
type and acting with this pointer. From this we can easily
recognize that mutable operations conflict with this const-
qualification. Static member functions don't have any this
pointer.

> In the same way, is there a provision
> to indicate that a static member function does not modify
> the static data members but only uses their values ?

In C++ there does not exist the concept of a class instance,
which would be necessary to make that approach reasonable.
*If* C++ would express classes itself as first-class objects
(some other languages do this with a so-called meta class
concept), than we would probably distinguish "pure" static
member functions (which are similar to free functions and
reflect the current concept of static function members) from
"class-static" functions, which would have a hidden pointer
to the meta class object (and not an individual this pointer).
For similar reasons C++ does not support virtual static
functions (which would make sense, if you consider
polymorphic meta-classes).
If you need such behaviour, you have to create your own
meta-class approach, which is solvable via pure language
means.

HTH & Greetings from Bremen,

Daniel Kr   gler


---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]