How do I control inline expansion of templates? 
Author Message
 How do I control inline expansion of templates?

Hi,

I've encountered the following "anomaly" with the VC6 SP3 compiler:

Member functions of class templates which are not defined inline are
sometimes nevertheless inlined by the compiler. The compiler options
are set to only expand functions marked with inline or __inline (Option
/Ob1). The #pragma auto_inline around the function definition does not
help either.

The funny thing is that I used the same non-inline member function
(with the same template parameters) three times in a single expression.
The first two were compiled to a proper function call, whilst the third
was expanded inline.

Clearly, the compiler has its own opinions about what to expand inline,
and they appear quite arbitrary to me. However, I'd really like to be
the ultimate authority in cases where I think it matters, so is there a
working way to twist the compiler's arm?

Thanks

Stefan

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 30 Jul 2002 03:00:00 GMT  
 How do I control inline expansion of templates?

Quote:

>Hi,

>I've encountered the following "anomaly" with the VC6 SP3 compiler:

>Member functions of class templates which are not defined inline are
>sometimes nevertheless inlined by the compiler. The compiler options
>are set to only expand functions marked with inline or __inline (Option
>/Ob1). The #pragma auto_inline around the function definition does not
>help either.

>The funny thing is that I used the same non-inline member function
>(with the same template parameters) three times in a single expression.
>The first two were compiled to a proper function call, whilst the third
>was expanded inline.

>Clearly, the compiler has its own opinions about what to expand inline,
>and they appear quite arbitrary to me. However, I'd really like to be
>the ultimate authority in cases where I think it matters, so is there a
>working way to twist the compiler's arm?

Have you tried compiling with /O1 instead of /O2?

--
Doug Harrison

Visual C++ MVP



Tue, 30 Jul 2002 03:00:00 GMT  
 How do I control inline expansion of templates?
Stefan,

"inline" is only ever a hint to the compiler.  You can disable or hint, end
of story I'm afraid.

The probable reason your template class's method gets inlined is that it is
implemented in with the class definition which automatically hints the
method as inline (standard ANSI C++).

Neil Groves
Lucid Computing


Quote:
> Hi,

> I've encountered the following "anomaly" with the VC6 SP3 compiler:

> Member functions of class templates which are not defined inline are
> sometimes nevertheless inlined by the compiler. The compiler options
> are set to only expand functions marked with inline or __inline (Option
> /Ob1). The #pragma auto_inline around the function definition does not
> help either.

> The funny thing is that I used the same non-inline member function
> (with the same template parameters) three times in a single expression.
> The first two were compiled to a proper function call, whilst the third
> was expanded inline.

> Clearly, the compiler has its own opinions about what to expand inline,
> and they appear quite arbitrary to me. However, I'd really like to be
> the ultimate authority in cases where I think it matters, so is there a
> working way to twist the compiler's arm?

> Thanks

> Stefan

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Wed, 31 Jul 2002 03:00:00 GMT  
 How do I control inline expansion of templates?

Quote:

>Stefan,

>"inline" is only ever a hint to the compiler.  You can disable or hint, end
>of story I'm afraid.

>The probable reason your template class's method gets inlined is that it is
>implemented in with the class definition which automatically hints the
>method as inline (standard ANSI C++).

VC++ tends to inline non-inline designated function templates that are
defined outside of class definitions, especially when you compile with
-O2. IIRC, VC2-4 were very aggressive about this, but using /O1
instead of /O2 suppresses most of this unwanted inline expansion in
VC5 and later.

--
Doug Harrison

Visual C++ MVP



Thu, 01 Aug 2002 03:00:00 GMT  
 How do I control inline expansion of templates?


Quote:

> >Stefan,

> >"inline" is only ever a hint to the compiler.  You can disable or
hint, end
> >of story I'm afraid.

I know that the compiler is allowed to not inline even though the
function is marked inline. My problem is the opposite: it inlines
although I tried to tell it not to. The question is still: how do you
disable inlining for a particular function in a class template without
disabling inlining globally?

Quote:
> >The probable reason your template class's method gets inlined is
that it is
> >implemented in with the class definition which automatically hints
the
> >method as inline (standard ANSI C++).

No. I'm well aware of that and that's not the reason.

Quote:
> VC++ tends to inline non-inline designated function templates that are
> defined outside of class definitions, especially when you compile with
> -O2. IIRC, VC2-4 were very aggressive about this, but using /O1
> instead of /O2 suppresses most of this unwanted inline expansion in
> VC5 and later.

It indeed behaves a lot more reasonable with /O1. Thanks for the tip.
However, as you have pointed out, the compiler still inline expands
occasionally (constructors seem to be its favorite candidates in my
code), although the code would be more compact if not inline expanded.

What it boils down to is that (at least) the documentation is wrong:

The description for the compiler option /Ob1 (Only __inline) states
that it "Expands only functions marked as inline or __inline or, in a
C++ member function, defined within a class declaration". When it comes
to templates, that's clearly not true.

Stefan

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 02 Aug 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How do I control inline expansion of templates?

2. non-inline template functions expanded inline

3. how do increase the chances of inline expansion?

4. Inline expansion versus threaded code

5. Help Wanted: Unwanted and Unexpected Inline Expansion

6. Template Expansions produce duplicate symbol errors in Linker

7. Template expansion errors from XMEMORY

8. Tool for template expansion

9. Automatic template expansion help

10. Problems with inline methods in a template class header

11. Inline templates

12. inline template functions missing at link time

 

 
Powered by phpBB® Forum Software