0

I read the following different sources,

  1. All the static data members are initialized before main() (even though the main() and static data members exist in different translation unit). – Agree
  2. The inline functions are duplicated in each source file or translation unit in which they are invoked. - Agree

It is clearly stated that static data members are initialized before main() then,

  1. What is the problem, if the inline functions access the static members?
  2. Why it is not safe to use static data members with in inline function?

Please explain with some clear examples.

  • "It is not safe to use static data members with in inline function" Where does it come from ? who do you quote ? – Stephane Rolland Dec 31 '13 at 11:31
  • Static function variable declaration are initialized(i.e. constructed) only on the first pass through the function (the first call). If the function is inlined... well... I still don't see why there'd be a problem. – StoryTeller - Unslander Monica Dec 31 '13 at 11:36
  • 1
    Also, why are constantly resisting to let people better format this question!? – StoryTeller - Unslander Monica Dec 31 '13 at 11:40
  • @StoryTeller Whether the function is inlined or not doesn’t matter (see as-if rule). Whether it is inline does because inline functions may be defined in multiple translation units. –  Dec 31 '13 at 11:40
  • @Stephane The question has been re-phrased. – user1099327 Dec 31 '13 at 11:40
  • @rightfold, so it doesn't matter, but it does matter!? :) – StoryTeller - Unslander Monica Dec 31 '13 at 11:41
  • @StoryTeller read it again until you see that “inlined” isn’t the same as “inline.” –  Dec 31 '13 at 11:41
  • 1
    Who says it's not safe? Did they give any more details on why it might not be? (Of course, it's not safe to access static data from a different translation unit *before* the start of `main`, or after the end, but that has nothing to do with inline functions.) – Mike Seymour Dec 31 '13 at 11:42
  • @StoryTeller I thought the question was not conveying what is required, that's Y i re-phrased it. Could you give me some examples with your answer for my better understanding? – user1099327 Dec 31 '13 at 11:47
  • @user1099327, see jcoders answer. He hit the nail on the head. – StoryTeller - Unslander Monica Dec 31 '13 at 11:48
  • @Mike how "the static data members can be accessed in different translation unit before start of main or after the end", could you provide some example? – user1099327 Dec 31 '13 at 11:52
  • @user1099327: Initialisers and constructors of variables with static storage duration run before the start of `main`, and their destructors run after the end, along with any functions registered with `atexit`. If any of these access static variables from other translation units, you're in for trouble. – Mike Seymour Dec 31 '13 at 11:55
  • @Mike Yes, I accept the static data members are initialized before the start of main(). But how these static variables can be accessed from another translation unit before main(). please explain? – user1099327 Dec 31 '13 at 12:03
  • @user1099327: As I said: they can be accessed by the initialiser of another static variable, which runs before `main`. But we're getting somewhat off-topic here; you still haven't described *why* you think there might be a problem accessing static variables from inline functions, and without that it's hard to answer the question beyond just stating "no, there's no problem". – Mike Seymour Dec 31 '13 at 12:06

1 Answers1

3

There is no problem using static members and inline methods.

You can find some similar discussion about static + inline here: static variables in an inlined function

Even if it talks about static variables, the mechanism is similar.

Community
  • 1
  • 1
Shay
  • 183
  • 2
  • 13