Click to See Complete Forum and Search --> : How to prevent a small function from being inline?


CppDev
November 26th, 2001, 04:25 AM
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.

James Curran
November 26th, 2001, 12:02 PM
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.

Andreas Masur
November 26th, 2001, 02:33 PM
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

CppDev
November 27th, 2001, 05:04 AM
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.

Kylin Li
November 27th, 2001, 06:55 AM
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

James Curran
November 27th, 2001, 07:50 AM
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.