templates and inline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Williams

    templates and inline

    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


  • Alf P. Steinbach

    #2
    Re: templates and inline

    On Thu, 07 Aug 2003 12:23:55 GMT, "Jeff Williams" <no_spam_remove _up_to_here_jwi lliams@mfchelp. com> wrote:
    [color=blue]
    >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<cla ss 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?[/color]

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


    [color=blue]
    >... how can I make the ones I want to be inline be inline.[/color]

    You cannot, but you can give the compiler a hint via 'inline'.

    Comment

    • tom_usenet

      #3
      Re: templates and inline

      On Thu, 07 Aug 2003 12:23:55 GMT, "Jeff Williams"
      <no_spam_remove _up_to_here_jwi lliams@mfchelp. com> wrote:
      [color=blue]
      >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<cla ss 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.[/color]

      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.
      [color=blue]
      >My thought is I can not declare my template class memebers within the class
      >declaration, but this is extremely tedious and hard to maintain.[/color]

      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

      Comment

      Working...