'static' for c++ class member functions? - c++

Why is the static keyword necessary at all?
Why can't the compiler infer whether it's 'static' or not?
As follows:
Can I compile this function without access to non-static member data? Yes -> static funcion.
No -> non-static function.
Is there any reason this isn't inferred?

If you expect the compiler to decide on the spot whether it's static or not, how does that affect external source files linking against the header file that just defines the method signatures?

The propery of being static or non-static affects the function type. Non-static member functions have an implicit this parameter, while static ones don't, for one example.
In other words, there's a major qualitative difference between static and non-static member functions. The compiler cannot "infer" this. This is a matter of the author's intent.
If I want (and need) my function to be non-static, I make it non-static, even if it doesn't access any non-static members of the class. If the compiler suddently decides to make my non-static function static just because it doesn't access any non-static members of the class, in general case it will destroy the functionality of the code.

Yes the compiler could, but it has no knowledge of your intent. And the original designers probably thought that supplying your intent was important.

Having redundancy in like this in the language helps to insure that many programmer errors will end up being caught by the compiler.

Another reason: If the function is static, it can't be overridden in derived classes. No polymorphism.

Related

Static data member with initializer

Assuming one is never odr-used, is there a benefit of declaring a static const or static constexpr data member with an initializer over declaring and then defining one later? Is there a benefit to not needing a definition?
First, note that you can't define a static const data member where you declare it. The declaration doesn't become a definition no matter what you do (such as providing an initializer). However, a static constexpr data member doesn’t need a definition, and the static const data member of integral type with initializer doesn’t need a definition if it’s never ODR-used.
ODR: the One Definition Rule, in the C++11 standard §3.2 [basic.def.odr].
Providing a definition is non-trivial in a header file – for a non-template class the direct approach would lead fast to violations of the ODR, with the linker complaining. Thus a benefit of not defining is that it makes it easy to use header-only modules. And a benefit of defining is that it makes it easy to use any type whatsoever, and supports ODR-use.
There are already a host of SO questions dealing with practical solutions to the requirement of definition for ODR use, with respect to header-only modules.
The ODR has a special exemption for templates for this, and that’s the basis of one practical solution. Just provide the definition in a class template, and use a dummy argument to instantiate the template. Another practical solution is to place the definition in a function, essentially a Meyers’ singleton.

Static callback function and non-static member

I actually have a code (which I can't change) needs a static function as "callback function".
When I change the function to static, I can't access "this" and my class members then. Also I can't pass "this" as parameter to function, again because I can't change the original SDK and definition files, so what can I do?
My code is in a class, one of class functions calls a function which needs a "static callback function" as parameter.
That "callback function" needs also to access "this" and other class members to process.
What can I do?
I'm using QT and VS 2012 as compiler.
Thanks
A static function is in common with a global function, u can't image that u can use "this" in a global function, and not to mention class members...
The only difference is a static function has a "namespace" as known as the calss name.
So, no matter what are you using or which compliler it is, you cannot use "this" or class members in a static function, it's just language features.
And you should modify your code as Mike says.
If you use modern versions of C++ (11 or older) you may use std::bind. Or use boost:bind.

static functions compiler optimization C++

I know that we should declare a function as static in a class if its a utility function or if we have to use in a singleton class to access private static member.
But apart from that does a static function provide any sort of compiler optimization too since it doesn't pass the "this" pointer? Why not just use the utility function through an already instantiated object of class? Or is it just a best practice to make utility functions as statics?
Thanks in advance.
The "non-passing-this" optimization can probably be done by the compiler once you turn the optimizations on. As I see it, a static function has rather idiomatic uses:
Implementing modules. There is a bit of overlap here with namespaces, and I would rather use namespaces.
Factories: you can make the constructor protected/private and then have several static functions (with different, explicit names) creating instances.
For function pointers: static functions do not require the slightly more complicated syntax of pointer to member function. That can be a plus when interacting with libraries written for C.
To keep yourself from using this and have the compiler to enforce it. It makes sense sometimes. For example, if you have a commutative operation that takes two instances, a static function that takes the two instances would emphasize (in my opinion) that the operation is commutative. Of course in many cases you would rather overload an operator.
In general a static function will ease namespace and "friend" clutter by prefixing an otherwise ordinary function with the name of a class, presumably because both are tightly related.
Static exists to associate a method with a class, rather than either:
Associating it with an instance of that class (like writing a normal, non-static member function).
Keeping it in the global namespace or whatever namespace you would otherwise be in (like declaring a function just in the file, not in a class).
Static says that 'conceptually this is something tied to/associated with this class, but it does not depend on any instance of that class'.
In more formal terms: a static member function is the same as a function declared outside of a class in all ways other than that it is part of that class's namespace and in that it has access rights to that class's private/protected data members.
Going back to your question:
There is no optimization gain here.
Utility function has nothing to do with it. It's whether or not it makes sense to scope the function in the class itself (rather than an instance of it).
It does not 'pass the this pointer' because there is no instance to speak of. You can call a static member function without ever invoking that class's constructor.

the position of the *static* keyword in member method declaration

Is there any difference between
class C {
static int func();
};
and
class C {
int static func();
};
I'm trying to remove the keyword static in someone else's code base. And I want to make sure I understand what the second example means before I do that.
[Edit] The reason to remove static: C was a "class" with no member variables and full of static methods. I think it's more proper to make "C" a namespace with normal functions instead of a class.
There is no difference. static on the function declaration applies to the function.
An this pointer will not be implicitly passed to this function, So you cannot access non static class members inside this function without explicitly passing the object to it.
To remove the static first you should know and understand the purpose that it is designed this way. Without taking that in to consideration you are just bound to create a code smell of vast proportions.
Yes, in the first case, the keyword static comes before the type int, in the second, they are the other way around. However, like many things in C and C++, there is no semantic difference. So, other than "cosmetically", there is no difference.
I'm not sure why you would want to remove static in classes as a general rule - there is probably a good reason a member function is declared static.

Can you inline static member functions?

I have a static member function which is merely syntactic sugar for me and I would like its body to appear in place of going through the motions of passing parameters to it. Will
inline static foo(int a) {return a & 0x00000040;}
be inlined just as it would if it was inline without being static?
The compiler chooses what it wants to do so we can't say what it will choose to do. That said, the function being static will not prevent it from being inlined; static functions are basically free functions with a different naming style and access to the class' private members.
A static member method has no this parameter, and can therefore only access static member variables.
It is distinct from whether the method is inlined or not. So the two are independent of each other.
The compiler decides if a method is going to be inlined or not. Your use of the keyword is merely a hint to the compiler.