templates and inline

Discussion in 'C++' started by Jeff Williams, Aug 7, 2003.

  1. The common method of defining template classes and functions is to put the
    definition and declaration into the same header file. Or at least I believe
    it to be the common method and it is certainly the one I use.

    This leaves me with a question.

    As example, consider the following class.

    template<class T>
    class foo
    {
    public:
    void my_big_func()
    {
    // non trivial code goes here that is something you wouldnt want
    // to be inline
    }
    };


    Now my question is, since all class functions declared and defined within
    the class declaration are taken to be inline, does that mean all my template
    class functions will be inline? If so how can I prevent this? and how can
    I make the ones I want to be inline be inline.

    My thought is I can not declare my template class memebers within the class
    declaration, but this is extremely tedious and hard to maintain.

    Jeff
     
    Jeff Williams, Aug 7, 2003
    #1
    1. Advertising

  2. On Thu, 07 Aug 2003 12:23:55 GMT, "Jeff Williams" <> wrote:

    >The common method of defining template classes and functions is to put the
    >definition and declaration into the same header file. Or at least I believe
    >it to be the common method and it is certainly the one I use.
    >
    >This leaves me with a question.
    >
    >As example, consider the following class.
    >
    >template<class T>
    >class foo
    >{
    >public:
    > void my_big_func()
    > {
    > // non trivial code goes here that is something you wouldnt want
    > // to be inline
    > }
    >};
    >
    >
    >Now my question is, since all class functions declared and defined within
    >the class declaration are taken to be inline, does that mean all my template
    >class functions will be inline?


    They're textually inline but not declared 'inline', and may or may not be
    inlined in the compiled program, at the compiler's discretion.



    >... how can I make the ones I want to be inline be inline.


    You cannot, but you can give the compiler a hint via 'inline'.
     
    Alf P. Steinbach, Aug 7, 2003
    #2
    1. Advertising

  3. Jeff Williams

    tom_usenet Guest

    On Thu, 07 Aug 2003 12:23:55 GMT, "Jeff Williams"
    <> wrote:

    >The common method of defining template classes and functions is to put the
    >definition and declaration into the same header file. Or at least I believe
    >it to be the common method and it is certainly the one I use.
    >
    >This leaves me with a question.
    >
    >As example, consider the following class.
    >
    >template<class T>
    >class foo
    >{
    >public:
    > void my_big_func()
    > {
    > // non trivial code goes here that is something you wouldnt want
    > // to be inline
    > }
    >};
    >
    >
    >Now my question is, since all class functions declared and defined within
    >the class declaration are taken to be inline, does that mean all my template
    >class functions will be inline? If so how can I prevent this? and how can
    >I make the ones I want to be inline be inline.


    Define the members outside the class definition, and use inline when
    you want inline. Some compilers think (or know) that they know better,
    and might ignore the use of inline, and not inline or inline at their
    own discretion.

    >My thought is I can not declare my template class memebers within the class
    >declaration, but this is extremely tedious and hard to maintain.


    You have to declare them in the class in C++, but you can define them
    outside. You can't call that hard to maintain, unless you always
    define all your methods inside the class definition for non-templates
    too!

    Tom
     
    tom_usenet, Aug 7, 2003
    #3
    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. Abhi
    Replies:
    2
    Views:
    751
    E. Robert Tisdale
    Jul 3, 2003
  2. JKop
    Replies:
    3
    Views:
    492
  3. Sean
    Replies:
    4
    Views:
    680
    Rolf Magnus
    Apr 30, 2006
  4. recover
    Replies:
    2
    Views:
    832
    recover
    Jul 25, 2006
  5. Rahul
    Replies:
    3
    Views:
    463
    James Kanze
    Feb 28, 2008
Loading...

Share This Page