static member function and const

S

subramanian100in

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. 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
 
A

Alf P. Steinbach

* (e-mail address removed), India:
why can't a static member function be declared as const ?

Best asked in [comp.std.c++], which deals with rationales, language
defects and new features; here we deal with using the language as-is.

We can
declare a non-static member function as const, to indicate that it
does not modify the non-mutable data members. 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 relevant language feature is a class, e.g.

struct Foo
{
struct Statics
{
double a;
bool b;

Statics(): a(), b() {};
double bar() const { return a*a; }
};

static Statics& statics()
{
static Statics theStatics; return theStatics;
}
};

Cheers, & hth.,

- Alf
 
K

Kibiz0r

why can't a static member function be declared as const ?

Because it wouldn't make any sense. A method is essentially the same
as a static function with one additional parameter, which is the
"this" variable. Making a method const just specifies that "this" is
const. It doesn't have anything to do with whether the function
changes any class data, per se, just whether "this" is const.

If you really *need* to specify that it can't change some data, put
the data in a separate container class and make the static function
take a const reference to the data. But I think it's more likely that
it's just not necessary. You write const methods so you can use them
with const instances, but that's irrelevant with static data.
 
K

Kira Yamato

why can't a static member function be declared as const ?

Because a static member function does not hold an implied object of
which its member variables can be modified (or its non-const methods be
called).
We can
declare a non-static member function as const, to indicate that it
does not modify the non-mutable data members. 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 ?

Yes. If you don't want a static member function to modify non-mutable
data (and to call non-const methods), then just declare the input
parameters const like this:

class T
{
public:
// func won't be able to modify 't'
static void func(const T &t);
};
 
A

Alf P. Steinbach

* Kibiz0r:
Because it wouldn't make any sense.

This is a common misconception where any missing language feature is
discussed.

E.g., it's also often offered as a purported explanation for the lack of
'virtual static' member function and for the lack of 'virtual' constructors.

But all it says is that the one offering this purported explanation is
unable to think of a sensible meaning.

In this case one possible sensible meaning for the construct was offered
by the OP, and furthermore the rest of your article makes clear that you
have read that part. And still you're unable to to imagine any sensible
meaning. One must conclude that you really think it is true that

that's irrelevant with static data

which is just nonsense.

Cheers, & hth.,

- Alf
 
K

Kibiz0r

Debating possible future meanings doesn't help answer the OP's
question.
Like you said, we have to deal with the language as-is.
 
J

James Kanze

* Kibiz0r:
This is a common misconception where any missing language feature is
discussed.

Wouldn't it be more accurate to say that the statement doesn't
have any real meaning. It's an almost meaningless phrases used
to lead into the real argument. If it's not followed up by
further argument, it's vacuous. If it is, it means whatever the
further argument says it means.

In this case, the poster was quite clear: allowing the keyword
const behind a static function makes no sense with regards to
the current semantics associated with const applied to a
function. Obviously, if you want to propose other semantics,
then someone opposing them would have to offer other arguments.
E.g., it's also often offered as a purported explanation for
the lack of 'virtual static' member function and for the lack
of 'virtual' constructors.

And for just about everything. That's a frequent case for more
or less vacuous lead-ins. They're just rhetoric (not
necessarily in the bad sense).
 
A

Alf P. Steinbach

* James Kanze:
Wouldn't it be more accurate to say that the statement doesn't
have any real meaning. It's an almost meaningless phrases used
to lead into the real argument. If it's not followed up by
further argument, it's vacuous. If it is, it means whatever the
further argument says it means.

In this case, the poster was quite clear: allowing the keyword
const behind a static function makes no sense with regards to
the current semantics associated with const applied to a
function. Obviously, if you want to propose other semantics,
then someone opposing them would have to offer other arguments.

Other semantics were proposed by the OP, and noted as such at the end in
the article I responded to.

What you write is, as I see it, mostly true, but that which seems to
refute points in my article bears no relation to that article.

And by some big coincidence (?), that was also the case for the article
I responded to, what there seemed to be a refutation of a point in the
OP's article had no relation to the OP's article.

Huh.

Cheers,

- Alf
 
J

Joe Greer

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. 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

I believe the biggest issue is that it isn't needed. Let me explain that.
Const member functions are required to flag member functions which are
callable on a const object. Note that this leaves you with the ability to
write to static member data and global data, just not member data. With
static functions, this requirement doesn't exist because there is no const
class data.

If you are looking for some form of protection against malicious
programmers, then you will need to look at a different language.
const_cast<> and other mechanisms like that allow the author to do what he
wants (pretty much anyway).

Have you run into a lot of static member functions that surprised you by
modifying static member data? That is, in my experience, the function name
gives a good clue as to whether it modifies data or not.

joe
 
J

Joel Yliluoma

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. 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?

A bit off-topic, but if you want to indicate that a function depends
strictly on its parameters and does not access any properties that
weren't passed as parameters, in GCC there's an extension called
"pure" that flags that. Example:

int timestwo(int n) __attribute__((pure));
int timestwo(int n) { return n*2; }

It helps the optimizer because now it knows that
timestwo(5)+timestwo(5) can be optimized as timestwo(5)*2
because the function does not have side effects.
There is no standard feature equivalent to this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top