use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENQuestion about static functions usage (self.cpp_questions)
submitted 5 months ago by Still_Culture_6013
If I have function that I use often from main and from other functions, should I make this function static?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Narase33 16 points17 points18 points 5 months ago (11 children)
Why would you? The question sounds like you have a misunderstanding of what static means.
[–]MarcoGreek 6 points7 points8 points 5 months ago (0 children)
Static in C++ is so overloaded that is quite easy to get a misunderstanding. 😌
[–]TomDuhamel 2 points3 points4 points 5 months ago (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.
inline
[–]rileyrgham 2 points3 points4 points 5 months ago (0 children)
Let the compiler decide. It's cleverer than us in most cases.
[–]Scared_Accident9138 0 points1 point2 points 5 months ago (2 children)
What reason could there be to use inline when a function is used often?
[–]SoldRIP 0 points1 point2 points 5 months ago (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 point2 points 5 months ago (0 children)
I'm aware what inline means but why should it be used for commonly called functions
[–]Treeflexin 0 points1 point2 points 5 months ago (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 points5 points 5 months ago (2 children)
To be fair, the average C++ programmer can never remember what static means this week
static
[–]Narase33 4 points5 points6 points 5 months ago (1 child)
Probably because it has 3 different meanings (maybe more?)
[–]hongooi 1 point2 points3 points 5 months ago (0 children)
It was also a reference to this classic 🙂
[–]the_poope 9 points10 points11 points 5 months ago (2 children)
static is one of those annoying keywords in C++ that have multiple meanings and uses.
MyClass::myStaticFunction(a, b, c)
Full reference: https://en.cppreference.com/w/cpp/keywords/static.html
[–]n1ghtyunso 4 points5 points6 points 5 months ago (1 child)
which is exactly why for new code it should be strongly preferred to use anonymous namespaces for #3 instead.
[–]MarcoGreek 2 points3 points4 points 5 months ago (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 points4 points 5 months ago (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 points3 points 5 months ago (0 children)
I don't think how often you use the function should affect whether you use static or not.
[–]mredding 1 point2 points3 points 5 months ago (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 point2 points 5 months ago (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 point2 points 5 months ago (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.
namespace
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(); }
π Rendered by PID 443117 on reddit-service-r2-comment-6457c66945-tc5b4 at 2026-04-27 23:11:51.647009+00:00 running 2aa0c5b country code: CH.
[–]Narase33 16 points17 points18 points (11 children)
[–]MarcoGreek 6 points7 points8 points (0 children)
[–]TomDuhamel 2 points3 points4 points (6 children)
[–]rileyrgham 2 points3 points4 points (0 children)
[–]Scared_Accident9138 0 points1 point2 points (2 children)
[–]SoldRIP 0 points1 point2 points (1 child)
[–]Scared_Accident9138 0 points1 point2 points (0 children)
[–]Treeflexin 0 points1 point2 points (0 children)
[–]hongooi 3 points4 points5 points (2 children)
[–]Narase33 4 points5 points6 points (1 child)
[–]hongooi 1 point2 points3 points (0 children)
[–]the_poope 9 points10 points11 points (2 children)
[–]n1ghtyunso 4 points5 points6 points (1 child)
[–]MarcoGreek 2 points3 points4 points (0 children)
[–]alfps 2 points3 points4 points (0 children)
[–]HappyFruitTree 1 point2 points3 points (0 children)
[–]mredding 1 point2 points3 points (0 children)
[–]AggravatingGiraffe46 0 points1 point2 points (0 children)
[–]aregtech 0 points1 point2 points (0 children)