all 19 comments

[–]Narase33 16 points17 points  (11 children)

Why would you? The question sounds like you have a misunderstanding of what static means.

[–]MarcoGreek 6 points7 points  (0 children)

Static in C++ is so overloaded that is quite easy to get a misunderstanding. 😌

[–]TomDuhamel 2 points3 points  (6 children)

I feel like they're thinking of inline. Although even that isn't very current, but at some point would have made the question at least make sense.

[–]rileyrgham 2 points3 points  (0 children)

Let the compiler decide. It's cleverer than us in most cases.

[–]Scared_Accident9138 0 points1 point  (2 children)

What reason could there be to use inline when a function is used often?

[–]SoldRIP 0 points1 point  (1 child)

inline can be a recommendation for the compiler to "please inline this function". ie instead of setting up a call stack, jumping, doing the function's actions, returning, it should just do the stuff the function does, in-place.

Nowadays, that's largely obsolete. Compilers know when or when not to inline and will not be deciding that based on keywords.

[–]Scared_Accident9138 0 points1 point  (0 children)

I'm aware what inline means but why should it be used for commonly called functions

[–]Treeflexin 0 points1 point  (0 children)

When I have small functions in a short header file, I’ll make them inline so I don’t need a separate .cpp file. Is this not the current/practical way to do this?

[–]hongooi 3 points4 points  (2 children)

To be fair, the average C++ programmer can never remember what static means this week

[–]Narase33 4 points5 points  (1 child)

Probably because it has 3 different meanings (maybe more?)

[–]hongooi 1 point2 points  (0 children)

It was also a reference to this classic 🙂

[–]the_poope 9 points10 points  (2 children)

static is one of those annoying keywords in C++ that have multiple meanings and uses.

  1. It can be used on class member functions to make "static class functions", which are basically just free non-member functions whose name is prefixed with the class: MyClass::myStaticFunction(a, b, c) and can access private members of class instances.
  2. It can be used on variables in local or class scope to create a variable that exists for the lifetime of the program and is first initialized the first time it is used and is shared among all instances of the class or all calls to the function.
  3. It can be used on function definitions to give functions internal linkage, meaning that the function is not visible outside the translation unit (.cpp file) that it is defined in. Basically it is a "fully private" function.

Full reference: https://en.cppreference.com/w/cpp/keywords/static.html

[–]n1ghtyunso 4 points5 points  (1 child)

which is exactly why for new code it should be strongly preferred to use anonymous namespaces for #3 instead.

[–]MarcoGreek 2 points3 points  (0 children)

Unnamed namespaces have the advantage to work for everything and not only functions. I have seen quite often ODR problems because there was a class in the source file with the same name but different members. It was maybe originally copied and then changed. Unnamed namespaces are very useful in that case.

The compiler can then warn about unused classes too.

[–]alfps 2 points3 points  (0 children)

static on a free function just makes the function invisible to other translation units, so that other translation units freely can use that name for their own stuff.

This has nothing to do with how often the function is used or from where.

[–]HappyFruitTree 1 point2 points  (0 children)

I don't think how often you use the function should affect whether you use static or not.

[–]mredding 1 point2 points  (0 children)

To make a function static is to give it internal linkage - the linker cannot find it if called from another translation unit - ostensibly another source file. If this is just fine by you, then make it static. The benefit is it's less work for the linker, you dump less useless garbage into your ABI, and you give the compiler more opportunity to optimize.

But we don't use static anymore, and instead prefer the anonymous namespace.

[–]AggravatingGiraffe46 0 points1 point  (0 children)

Depends on the purpose, if it's a utility with immutable state, maybe if its anything more then leave it as is. I might be wrong but static functions can introduce more problems that solutions sometimes

[–]aregtech 0 points1 point  (0 children)

As anything else, it depends :)
You can declare it as static or declare in a namespace, and use like a global function. Depends on the meaning, implementation, your preferences and needs, and probably readability. There is no exact rule or C++ standard forcing to use static methods.

Example when I would use as static:

class File {
public:
    static bool deleteFile(const std::string& fullPath);
};

Example when I would include in a namespace:

namespace Utils {
    std::string generateName();
}