Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6921875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:19:59+00:00 2026-05-27T10:19:59+00:00

I’ve recently seen a bit on SO about the static keyword before a function

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:20:00+00:00Added an answer on May 27, 2026 at 10:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.