are all templates inlined?

Discussion in 'C++' started by sks, Dec 6, 2005.

  1. sks

    sks Guest

    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.
     
    sks, Dec 6, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Dec 6, 2005
    #2
    1. Advertising

  3. sks

    benben Guest

    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
     
    benben, Dec 6, 2005
    #3
  4. sks

    sks Guest

    "Victor Bazarov" <> wrote in message
    news:...
    > 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.
     
    sks, Dec 6, 2005
    #4
  5. sks wrote:
    > [...]
    > So, can I conclude that templates SOMETIMES are not inlined?


    Sure.

    V
     
    Victor Bazarov, Dec 6, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    3
    Views:
    297
  2. JKop
    Replies:
    3
    Views:
    484
  3. recover
    Replies:
    2
    Views:
    820
    recover
    Jul 25, 2006
  4. newbie
    Replies:
    2
    Views:
    388
    Dave Rahardja
    Apr 28, 2007
  5. Michal
    Replies:
    4
    Views:
    346
    Jorgen Grahn
    Nov 22, 2008
Loading...

Share This Page