duplicate definitions with template member function

Discussion in 'C++' started by mathieu, Dec 18, 2007.

  1. mathieu

    mathieu Guest

    Why is the following code causing duplicate definitions (gcc 4.2) when
    including swapper.h in multiple files ?

    Thanks
    -Mathieu

    --------swapper.h-----------
    #ifndef swapper_h
    #define swapper_h

    class Swapper
    {
    public:
    template <typename T> static T Swap(T val);
    template <typename T>
    static void SwapArray(T *array, unsigned int n)
    {
    for(unsigned int i = 0; i < n; ++i)
    {
    array = Swap<T>(array);
    }
    }
    };
    #include "swapper.txx"
    #endif

    --------swapper.txx-----------
    #ifndef swapper_txx
    #define swapper_txx

    template <> uint16_t Swapper::Swap<uint16_t>(uint16_t val)
    {
    return bswap_16(val);
    }
    template <> uint32_t Swapper::Swap<uint32_t>(uint32_t val)
    {
    return bswap_32(val);
    }
    template <> uint64_t Swapper::Swap<uint64_t>(uint64_t val)
    {
    return bswap_64(val);
    }

    #endif
     
    mathieu, Dec 18, 2007
    #1
    1. Advertising

  2. mathieu wrote:
    > Why is the following code causing duplicate definitions (gcc 4.2) when
    > including swapper.h in multiple files ?


    Because 'spapper.txx' contains definitions (explicit specialisations).
    Those aren't templates, and as such need to be placed in a translation
    unit instead of a header which is included in more than one TU.

    >
    > Thanks
    > -Mathieu
    >
    > --------swapper.h-----------
    > #ifndef swapper_h
    > #define swapper_h
    >
    > class Swapper
    > {
    > public:
    > template <typename T> static T Swap(T val);
    > template <typename T>
    > static void SwapArray(T *array, unsigned int n)
    > {
    > for(unsigned int i = 0; i < n; ++i)
    > {
    > array = Swap<T>(array);
    > }
    > }
    > };
    > #include "swapper.txx"
    > #endif
    >
    > --------swapper.txx-----------
    > #ifndef swapper_txx
    > #define swapper_txx
    >
    > template <> uint16_t Swapper::Swap<uint16_t>(uint16_t val)
    > {
    > return bswap_16(val);
    > }
    > template <> uint32_t Swapper::Swap<uint32_t>(uint32_t val)
    > {
    > return bswap_32(val);
    > }
    > template <> uint64_t Swapper::Swap<uint64_t>(uint64_t val)
    > {
    > return bswap_64(val);
    > }
    >
    > #endif


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 18, 2007
    #2
    1. Advertising

  3. mathieu

    mathieu Guest

    On 18 déc, 16:37, "Victor Bazarov" <> wrote:
    > mathieu wrote:
    > > Why is the following code causing duplicate definitions (gcc 4.2) when
    > > including swapper.h in multiple files ?

    >
    > Because 'spapper.txx' contains definitions (explicit specialisations).
    > Those aren't templates, and as such need to be placed in a translation
    > unit instead of a header which is included in more than one TU.


    Ah of course ! Thanks Victor.

    Well the issue here is that I am trying to avoid a function call,
    since the bswap* will get called in a for loop.

    Thanks again,
    -Mathieu
     
    mathieu, Dec 18, 2007
    #3
  4. mathieu wrote:
    > On 18 déc, 16:37, "Victor Bazarov" <> wrote:
    >> mathieu wrote:
    >>> Why is the following code causing duplicate definitions (gcc 4.2)
    >>> when including swapper.h in multiple files ?

    >>
    >> Because 'spapper.txx' contains definitions (explicit
    >> specialisations). Those aren't templates, and as such need to be
    >> placed in a translation unit instead of a header which is included
    >> in more than one TU.

    >
    > Ah of course ! Thanks Victor.
    >
    > Well the issue here is that I am trying to avoid a function call,
    > since the bswap* will get called in a for loop.


    Try declaring those specialisations "inline".

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Dec 18, 2007
    #4
  5. mathieu

    mathieu Guest

    On 18 déc, 17:28, "Victor Bazarov" <> wrote:
    > mathieu wrote:
    > > On 18 déc, 16:37, "Victor Bazarov" <> wrote:
    > >> mathieu wrote:
    > >>> Why is the following code causing duplicate definitions (gcc 4.2)
    > >>> when including swapper.h in multiple files ?

    >
    > >> Because 'spapper.txx' contains definitions (explicit
    > >> specialisations). Those aren't templates, and as such need to be
    > >> placed in a translation unit instead of a header which is included
    > >> in more than one TU.

    >
    > > Ah of course ! Thanks Victor.

    >
    > > Well the issue here is that I am trying to avoid a function call,
    > > since the bswap* will get called in a for loop.

    >
    > Try declaring those specialisations "inline".


    Cool ! This is at least working for my compiler :)

    Thanks
    -Mathieu
     
    mathieu, Dec 18, 2007
    #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. Roedy Green
    Replies:
    4
    Views:
    467
  2. Replies:
    1
    Views:
    484
    Victor Bazarov
    Jul 20, 2005
  3. Markus Kern
    Replies:
    9
    Views:
    445
  4. Peng Yu
    Replies:
    3
    Views:
    806
    Thomas J. Gritzan
    Oct 26, 2008
  5. johkar
    Replies:
    0
    Views:
    1,955
    johkar
    Dec 16, 2009
Loading...

Share This Page