CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2000
    Location
    Greece
    Posts
    94

    How to prevent a small function from being inline?

    Hi,
    How can I prevent a very small function from being inline?
    I have to put that function in the header file because belongs to a template class, and I must ensure that it's not inline because contains a static member variable that must be unique through the program.


  2. #2
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: How to prevent a small function from being inline?

    First of all, the compile should be able to handle the unique static. If not, the compiler's broken.

    Otherwise, there's not Standardized way of saying "don't inline". You either specifically say to inline or you don't. In the latter case, it's pure implementation defined whether or not it's inlined.

    In MSVC++, use a pragma:
    #pragma auto_inline(off)
    void non_inlined_func()
    {
    }
    #pragma auto_inline() // returns to previous state





    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How to prevent a small function from being inline?

    Change the following compiler setting within the project settings (ATL+F7):

    Register C/C++ -> Category Optimizations -> Inline function expansion: Disable

    That should do the trick...

    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  4. #4
    Join Date
    Mar 2000
    Location
    Greece
    Posts
    94

    Re: How to prevent a small function from being inline?

    Applying the following code in release build, I don't think that it works. Any ideas why?
    (I use MSVC++ 6.0 + SP5)

    template <class T>
    class Template_class
    {
    public:
    static int& get();
    };

    #pragma auto_inline(off)
    template <class T>
    int& Template_class<T>::get() { static int the_static_variable=0; return the_static_variable; }
    #pragma auto_inline()



    Thanks for replying.


  5. #5
    Join Date
    Oct 2001
    Location
    Chongqing China
    Posts
    15

    Re: How to prevent a small function from being inline?

    I think your codes doesn't involve "inline".whether you use #pragma auto_inline() or not, it cannot work. In your case, when a new instantiation of template_class was created, a new "static the_static_variable" then be created.
    so ,for example, template_class<int> and template_class<double> will both have a static variable "the_static_variable". They can be accessed by template_class<int>::get() and template_class<double>::get() respectively, and these two variable are independent from each other.

    Since going backward is not the answer,how do we move forward?
    Kylin Li

  6. #6
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: How to prevent a small function from being inline?

    Try putting the #pragma around where the class is instaniated:#pragma auto_inline(off)
    Template_class<int> int_Temp;
    #pragma auto_inline()






    Truth,
    James
    http://www.NJTheater.com
    http://www.NovelTheory.com
    I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured