Where should the body of template specialisations be placed - header or source file?

Discussion in 'C++' started by Kevin Frey, Sep 9, 2003.

  1. Kevin Frey

    Kevin Frey Guest

    Hello,

    Let's say I have the following:

    template< class T > class MyClass
    {
    void Func( );
    };

    and I want to declare a specialisation for Func( ) in the presence of
    a particular type, eg:

    template< > void MyClass< long >::Func( )
    {
    // body of function here
    }

    Whereabouts should the above specialisation be placed - in the header,
    or in the source file (with a corresponding prototype declared as
    follows):

    template< > void MyClass< long >::Func( );

    Thanks

    Kevin.
     
    Kevin Frey, Sep 9, 2003
    #1
    1. Advertising

  2. Kevin Frey

    Artie Gold Guest

    Re: Where should the body of template specialisations be placed -header or source file?

    Kevin Frey wrote:
    > Hello,
    >
    > Let's say I have the following:
    >
    > template< class T > class MyClass
    > {
    > void Func( );
    > };
    >
    > and I want to declare a specialisation for Func( ) in the presence of
    > a particular type, eg:
    >
    > template< > void MyClass< long >::Func( )
    > {
    > // body of function here
    > }
    >
    > Whereabouts should the above specialisation be placed - in the header,
    > or in the source file (with a corresponding prototype declared as
    > follows):
    >
    > template< > void MyClass< long >::Func( );
    >

    Unless your compiler supports the `export' keyword (which is most
    likely not the case, very few do), you'll have to put it in your header.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Sep 9, 2003
    #2
    1. Advertising

  3. Kevin Frey

    tom_usenet Guest

    On 8 Sep 2003 17:41:41 -0700, (Kevin Frey)
    wrote:

    >Hello,
    >
    >Let's say I have the following:
    >
    >template< class T > class MyClass
    >{
    > void Func( );
    >};
    >
    >and I want to declare a specialisation for Func( ) in the presence of
    >a particular type, eg:
    >
    >template< > void MyClass< long >::Func( )
    >{
    > // body of function here
    >}
    >
    >Whereabouts should the above specialisation be placed - in the header,
    >or in the source file (with a corresponding prototype declared as
    >follows):
    >
    >template< > void MyClass< long >::Func( );


    If you don't want it to be inline, you have to put the definition in
    the source file (but make sure the declaration is in the header to
    avoid linker errors/undefined behaviour). If you want it to be inline,
    use the inline keyword with the definition and put it in the header.

    Basically, function specializations (and explicit instantiations) must
    obey the one definition rule, just like non-templates.

    Tom
     
    tom_usenet, Sep 9, 2003
    #3
  4. (Kevin Frey) wrote in message news:<>...
    > Hello,
    >
    > Let's say I have the following:
    >
    > template< class T > class MyClass
    > {
    > void Func( );
    > };
    >
    > and I want to declare a specialisation for Func( ) in the presence of
    > a particular type, eg:
    >
    > template< > void MyClass< long >::Func( )
    > {
    > // body of function here
    > }
    >
    > Whereabouts should the above specialisation be placed - in the header,
    > or in the source file (with a corresponding prototype declared as
    > follows):
    >
    > template< > void MyClass< long >::Func( );
    >


    Your explicit specialization should be defined in your source file,
    and declared in your header file. Or, if you're lazy, you can just
    write in one file

    template< class T > class MyClass
    {
    void Func( );
    };

    template< > void MyClass< long >::Func( )
    {
    // body of function here
    }

    and let a program generate your header and source files. See
    http://www.lazycplusplus.net. Try it out at
    http://www.lazycplusplus.net/cgi-bin/lzzcgi.

    Mike
     
    Michael Spencer, Sep 9, 2003
    #4
    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. upendra
    Replies:
    0
    Views:
    4,324
    upendra
    Aug 31, 2006
  2. vivekian
    Replies:
    9
    Views:
    294
  3. Replies:
    5
    Views:
    318
  4. AG
    Replies:
    6
    Views:
    312
    Keith Thompson
    Aug 13, 2007
  5. dan
    Replies:
    11
    Views:
    1,019
Loading...

Share This Page