Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I write the keyword 'static' before a non-member function?

Tags:

c++

static

linker

I've recently seen a bit on SO about the static keyword before a function and I'm wondering how to use it properly.

1) When should I write the keyword static before a non-member function?

2) Is it dangerous to define a static non-member function in the header? Why (not)?


(Side Question)

3) Is it possible to define a class in the header file in a certain way, so that it would only be available in the translation unit where you use it first?

(The reason that I'm asking this is because I'm learning STL and it might be a good solution for my predicates etc (possibly functors), since I don't like to define functions other than member-functions in the cpp file)

(Also, I think it is related in a way to the original question because according to my current reasoning, it would do the same thing as static before a function does)

EDIT

Another question that came up while seeing some answers:

4) Many people tell me I have to declare the static function in the header, and define it in the source file. But the static function is unique to the translation unit. How can the linker know which translation unit it is unique to, since header files do not directly relate to a source file (only when you include them)?

like image 743
xcrypt Avatar asked Dec 06 '11 21:12

xcrypt


People also ask

When should I declare a function static?

A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

When would you use a non member function?

Use a nonmember function if you don't need type conversion in the first argument or don't need access to private data. Use a member function if you do need access to private data.

Can static function use non-static members?

A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members.

Why this keyword is not applicable in static member functions?

Which among the following is not applicable for the static member functions? Explanation: Since the static members are not property of objects, they doesn't have this pointer. Every time the same member is referred from all the objects, hence use of this pointer is of no use.


1 Answers

static, as I think you're using it, is a means of symbol hiding. Functions declared static are not given global visibility (a Unix-like nm will show these as 't' rather than 'T'). These functions cannot be called from other translation units.

For C++, static in this sense has been replaced, more or less, by the anonymous namespace, e.g.,

static int x = 0; 

is pretty equivalent to

namespace {   int x = 0; } 

Note that the anonymous namespace is unique for every compilation unit.

Unlike static, the anonymous namespace also works for classes. You can say something like

namespace {  class Foo{}; } 

and reuse that class name for unrelated classes in other translation units. I think this goes to your point 3.

The compiler actually gives each of the symbols you define this way a unique name (I think it includes the compilation time). These symbols are never available to another translation unit and will never collide with a symbol from another translation unit.

Note that all non-member functions declared to be inline are also by default static. That's the most common (and implicit) use of static. As to point 2, defining a static but not inline function in a header is a pretty corner case: it's not dangerous per se but it's so rarely useful it might be confusing. Such a function might or might not be emitted in every translation unit. A compiler might generate warnings if you never actually call the function in some TUs. And if that static function has within it a static variable, you get a separate variable per translation unit even with one definition in a single .h which might be confusing. There just aren't many (non-inline) use cases.

As to point 4, I suspect those people are conflating the static member function meaning of static with that of the linkage meaning of static. Which is as good a reason as any for using the anonymous namespace for the latter.

like image 145
smparkes Avatar answered Sep 20 '22 03:09

smparkes