9

Possible Duplicate:
C++ - Why static member function can’t be created with ‘const’ qualifier

Was curious to know the reason why static member functions cant be declared as const or volatile or const volatile ?

#include<iostream>

class Test 
{     
   static void fun() const 
   { // compiler error
       return;
   }
};
Community
  • 1
  • 1
Laavaa
  • 575
  • 9
  • 19

4 Answers4

14

The cv-modifiers of the member functions correspond to the qualification of the hidden this parameter.

static functions have no this parameter. Therefore, they need no cv-qualifiers. So it was decided (IMHO rightly, as otherwise, it would have no meaning) to disallow them on static functions.

BTW static member functions can't also be virtual, pure (=0), deleted, defaulted, && etc.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
  • Are you sure about **deleted** ? Removing a specific overload is a common use of `delete` and I would expect the need to show up for `static` functions as well. – Matthieu M. Oct 21 '12 at 13:39
11

Because that's what the standard says:

9.4.1 Static member functions [class.static.mfct]

2) [ Note: A static member function does not have a this pointer (9.3.2). —end note ] A static member function shall not be virtual. There shall not be a static and a non-static member function with the same name and the same parameter types (13.1). A static member function shall not be declared const, volatile, or const volatile. (emphasis mine)

The reason for this is that a const (or volatile or virtual) static method wouldn't make sense (in the traditional sense, see below). For example, const implies you can't modify the object's members, but in the case of statics, there's no object to talk about.

You could argue that a const static could apply to other static members, but this option was regarded as pointless.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • @Luchian Grigore Thank you for the answer, Could you also please put some light on 'You could argue that a const static could apply to other static members, but this option was regarded as pointless' - Why would this be pointless? – Starfish Apr 11 '17 at 08:33
  • @Starfish can you think of a situation where it would be useful? – Luchian Grigore Apr 11 '17 at 14:55
1

Static member functions are global, free functions that do not depend on an object (i.e. a class instance). The CV qualifiers on non-static member functions refer to the type of the object on which they are called, and this notion simply doesn't apply to static member functions.

Example:

struct Foo
{
    void f();        // Overload #1
    void f() const;  // Overload #2
    static void g();
};

int main()
{
    Foo x;

    x.f();                            // calls overload #1
    static_cast<Foo const &>(x).f();  // calls overload #2

    Foo::g();                         // does not know about any instance!

    x.g();                            // same as Foo::g(); "x" is a red herring
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

There is no "current object" when executing a static function so it makes little sense to discuss about static functions being const or not.

Note that you can call a static function using an instance, but that's just a strange C++ "feature" (sometimes handy because in C++03 it was hard to get the type of a value).

struct Foo {
    static void f();
};

void bar()
{
    Foo foo_instance;
    foo_instance.f();  // Exactly the same as Foo::f()
}

I can understand that you would like to be able to describe "const static functions" that don't alter any static data member and that can also only call other const static functions.

But this distinction is not present in C++.

6502
  • 112,025
  • 15
  • 165
  • 265