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.

14
  • "It is not safe to use static data members with in inline function" Where does it come from ? who do you quote ? Commented Dec 31, 2013 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. Commented Dec 31, 2013 at 11:36
  • 1
    Also, why are constantly resisting to let people better format this question!? Commented Dec 31, 2013 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. Commented Dec 31, 2013 at 11:40
  • 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.) Commented Dec 31, 2013 at 11:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.