0

I read so many answers saying static const functions can't exist. Eg. this Q&A. and this Q&A

What if this is the use case:

#include<iostream>
using namespace std;
class Sample{
   static const int privx;
   static int privy;
   
   public:
   static int getPrivx(){
       return privx;
   }
   static int getPrivy(){
      // privy += 100;// I want to avoid changes in static variables through a static function => I need const static function
       return privy;
   }
};
const int Sample::privx = 90;
int Sample::privy = 100;

int main(){
   cout<<Sample::getPrivx()<<endl;
   cout<<Sample::getPrivy()<<endl;
}

So, use case: There are a lot of static variables declared in class. During development phase, I want to prevent developer from manipulating values of these static variables via operations inside a (static) function. Basically, compiler must shoot error saying changing value in static variable not allowed. The exact reason why const methods are used for i.e. no manipulations inside it (to calling object members of course).

What are your solutions? I think a static & const function must be allowed as:

static int getPrivy() const{}

P.S. I asked this question before without considering const functions syntax => hence readers may find unrelated answers. Apologies.

Abhinav Gupta
  • 33
  • 1
  • 6
  • 2
    The quote is talking about *const qualified member functions* not const return types. E.g. `static void fun() const {`. See where the `const` is? It makes the method a const method, it's not about the return type begin const. And the quote is perfectly correct static methods cannot be const methods, but they can have const return types. – john Jul 09 '22 at 08:36
  • Closely related: https://stackoverflow.com/questions/5057987/whats-the-point-of-const-void Note: the `const` is not a modifier of the function, but is part of the return type here. – fabian Jul 09 '22 at 08:37
  • Also the quote says nothing at all about member variables. Member variables can be const and static. – john Jul 09 '22 at 08:39
  • In your defense, keywords like `static`, `const`, `inline`, `register`, and others have changed their meaning over time and/or added new meanings for particular contexts. Keeping all of them straight in one's head in all the different contexts is challenging. Also, some are implied, in a given context... as Kate Gregory mentioned in one of her presentations "All the defaults are wrong". Why did they get repurposed? To avoid adding new keywords to the language, which has a chance of breaking existing code. – Eljay Jul 09 '22 at 17:28
  • I have corrected the syntax. Kindly help in case of above use case @everyone – Abhinav Gupta Jul 09 '22 at 20:33
  • 1
    @AbhinavGupta There is no mechanism in C++ to do exactly what you are asking. But what you could do instead is put all those static variables and methods into another class (called say `Globals`) as regular members and methods, You then define one instance of that class statically inside `Sample`. Now what used to be static methods in `Sample` are now non-static methods in `Globals`, so they can be declared `const` in the usual way. – john Jul 09 '22 at 20:39
  • This soln. seems perfect @john. Thanks. But still what is the logic we don't have static const functions? – Abhinav Gupta Jul 09 '22 at 21:01
  • @AbhinavGupta It's simply the role that `const` plays in C++. `const` is a type qualifier. In the case of a member function the type being modified is the `this` pointer. In the case of a static function there is no type to be modified so `const` cannot be used. What you are asking for is more like an [attribute](https://en.cppreference.com/w/cpp/language/attributes) and although there are a number of attributes for functions I've never seen one that does what you are asking for. – john Jul 10 '22 at 05:01

2 Answers2

2

This code is illegal, it's const qualified static method

class Sample{
   public:
   static int fun() const {
       return 0;
   }
};

This code is OK, it's a const qualified non-static method

class Sample{
   public:
   int fun() const {
       return 0;
   }
};

This code is OK, it's a static method with a const return type

class Sample{
   public:
   static const int fun() {
       return 0;
   }
};

I guess the problem is that you don't realise what it means that a method is const. It has nothing to do with the return type, it's about the type of the this pointer within the method (as the quote says).

john
  • 85,011
  • 4
  • 57
  • 81
1
static const int L = 90, B = 80;

There is nothing wrong with static const member variables. No idea where you get the idea from that this is not allowed.


static const void fun(){
   cout<<"fun="<<L*B<<endl;
}

This is not a const-qualified function. It is an unqualified static member function with const return type, specifically return type const void.

You can't have a const-qualified static member function, e.g. something like

static void fun() const {
   cout<<"fun="<<L*B<<endl;
}

but const return types are always allowed (although mostly useless, especially with void).

user17732522
  • 53,019
  • 2
  • 56
  • 105