Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > are all templates inlined?

Reply
Thread Tools

are all templates inlined?

 
 
sks
Guest
Posts: n/a
 
      12-06-2005
All,

I thought all template instantiations were inlined until I analyzed the
following code.

template<typename t>
class Something
{
public:
inline int getSomething() { return 1; }
int getAnother();
};

template<typename t>
int Something<t>::getAnother()
{
return 2;
}

(I may not have the syntax exactly correct here since I am not
copying/pasting the code that I tested at work today).

Anyway, when I analyzed the assembly code, the compiler generated a symbol
for "getSomething" but there wasn't one for "getAnother" (it seemed to be
inlined).

Can we conclude from this that all templates are not inlined?

Thanks.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-06-2005
sks wrote:
> I thought all template instantiations were inlined until I analyzed
> the following code.
>
> template<typename t>
> class Something
> {
> public:
> inline int getSomething() { return 1; }


'inline' here is superfluous. Any member function defined in the class
definition is inline.

> int getAnother();
> };
>
> template<typename t>
> int Something<t>::getAnother()
> {
> return 2;
> }
>
> (I may not have the syntax exactly correct here since I am not
> copying/pasting the code that I tested at work today).


I thin you got the right syntax.

> Anyway, when I analyzed the assembly code, the compiler generated a
> symbol for "getSomething" but there wasn't one for "getAnother" (it
> seemed to be inlined).


That's of no consequence here.

> Can we conclude from this that all templates are not inlined?


No. Whatever one particular implementation does is immaterial, really.

Besides, when compiling this particular code, the compiler could throw
everything away. The code is not _used_, so the template is never
_instantiated_.

V


 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      12-06-2005
sks wrote:
> All,
>
> I thought all template instantiations were inlined until I analyzed the
> following code.
>
> template<typename t>
> class Something
> {
> public:
> inline int getSomething() { return 1; }
> int getAnother();
> };
>
> template<typename t>
> int Something<t>::getAnother()
> {
> return 2;
> }
>
> (I may not have the syntax exactly correct here since I am not
> copying/pasting the code that I tested at work today).
>
> Anyway, when I analyzed the assembly code, the compiler generated a symbol
> for "getSomething" but there wasn't one for "getAnother" (it seemed to be
> inlined).


Template instantiation is just like anything else--they are inline when
the compiler feels like to inline them.

>
> Can we conclude from this that all templates are not inlined?


No. You generally cannot conclude base or symbol table.

>
> Thanks.
>
>


Ben
 
Reply With Quote
 
sks
Guest
Posts: n/a
 
      12-06-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> sks wrote:
>> I thought all template instantiations were inlined until I analyzed
>> the following code.
>>
>> template<typename t>
>> class Something
>> {
>> public:
>> inline int getSomething() { return 1; }

>
> 'inline' here is superfluous. Any member function defined in the class
> definition is inline.
>
>> int getAnother();
>> };
>>
>> template<typename t>
>> int Something<t>::getAnother()
>> {
>> return 2;
>> }
>>
>> (I may not have the syntax exactly correct here since I am not
>> copying/pasting the code that I tested at work today).

>
> I thin you got the right syntax.
>
>> Anyway, when I analyzed the assembly code, the compiler generated a
>> symbol for "getSomething" but there wasn't one for "getAnother" (it
>> seemed to be inlined).

>
> That's of no consequence here.
>
>> Can we conclude from this that all templates are not inlined?

>
> No. Whatever one particular implementation does is immaterial, really.
>
> Besides, when compiling this particular code, the compiler could throw
> everything away. The code is not _used_, so the template is never
> _instantiated_.
>
> V


I do call both methods for a particular template instantiation though.


I made a mistake on my original post. One of my paragraphs should have said:
' Anyway, when I analyzed the assembly code, the compiler DID NOT GENERATE a
symbol for "getSomething" but GENERATED ONE for "getAnother" '.


So, can I conclude that templates SOMETIMES are not inlined?

If I used the above approach of separating the implementation of a member
function from the class definition, then the symbol is generated for it,
however, if the implementation of the method is defined within the class
definition, like I did above, then it seemed to be inlined.

Thanks.




 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-06-2005
sks wrote:
> [...]
> So, can I conclude that templates SOMETIMES are not inlined?


Sure.

V


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments