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 6064127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:14:47+00:00 2026-05-23T09:14:47+00:00

inline Config& config() { static Config *c = new Config(); return *c; } The

  • 0
inline Config& config()
{
    static Config *c = new Config();
    return *c;
}

The function above returns a pointer to class Config, created once when function calls.

Will the C++ compiler be able to inline this function correctly?

I mean c is static object, and creating it at first time will lead to inlined new Config() somewhere in the code. But when the function is called second time, what will be at the place of the config()? Inlined c? Or a function call?

  • 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-23T09:14:47+00:00Added an answer on May 23, 2026 at 9:14 am

    You seem to have a slight misunderstanding about how such static variables work. It seems like you’re thinking the compiler emits one set of code the first time the function is called, and another set every other time. That’s not the case. You might consider the following transformation.

    bool initialized = false;
    Config* c;
    
    inline Config& config() {
        if(!initialized) {
            c = new Config();
            initialized = true;
        }
        return *c;
    }
    

    That’s a simplification, but it gets the point across. The function tracks whether or not the static has been initialized, and if it hasn’t, does so. It does this check every time you call the function.

    With that in mind, the existance of a static variable has no direct impact on the inlinability of a particular function… the compiler will simply inline the check along with everything else. The question is simply, does this new expanded code still meet the requirements the compiler sets forth for inlining a function? It might not, but either way the visible result should be the same.

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

Sidebar

Related Questions

inline int factorial(int n) { if(!n) return 1; else return n*factorial(n-1); } As I
VC++ makes functions which are implemented within the class declaration inline functions. If I
Why are inline closures so rarely used in Actionscript? They are very powerful and
Is it possible to embed an inline search box into a web page which
How can I document a member inline in .Net? Let me explain. Most tools
What is the advantages/disadvantages of using inline functions in C++? I see that it
Does anyone know of a good sample (inline in reply, or web tutorial) for
Is it possible to create an inline delegate in vb.net like you can in
I am using VB9's inline XML, and I have a need to specify a
I have a import directive in my inline .aspx page (no code-behind). Getting an

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.