user1628622
user1628622

Reputation:

Should static member functions be avoided in shared libraries?

In reading the book "Writing Scientific Software" by Oliveira and Stewart, I came across this interesting passage:

"Shared variables are dangerous and should be avoided in shared libraries

So if you are writing a shared or dynamically linked library, avoid static or saved local variables and avoid global variables."

(page 55)

But what about static member functions? Are these equally as dangerous in a shared library? Should I avoid these as well? Why/why not?

Upvotes: 6

Views: 1592

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726699

But what about static member functions? Are these equally as dangerous in a shared library?

Not at all: static member functions should not be avoided: unlike static variables that represent shared state, static member functions represent shared computations. As long as these computations are stateless, they are not dangerous at all.

Upvotes: 4

BeeOnRope
BeeOnRope

Reputation: 64955

Not inherently. A static member function which doesn't use any static state (e.g. ones that use only local or thread-local variables) isn't unsafe. Such methods are frequently used in well respected libraries such as boost or guava (that last example from the java world).

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 993451

Static functions are not a problem. In fact, many of the functions one would commonly use in scientific software are pure math functions such as sin(), sqrt(), log().

Static variables, on the other hand, are an indicator of shared state and should be avoided.

Upvotes: 3

Related Questions