| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"mathieu" |
| Date: |
18 Dec 2007 09:00:19 AM |
| Object: |
duplicate definitions with template member function |
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[i] = Swap<T>(array[i]);
}
}
};
#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
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: duplicate definitions with template member function |
18 Dec 2007 09:37:45 AM |
|
|
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[i] = Swap<T>(array[i]);
}
}
};
#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
.
|
|
|
| User: "mathieu" |
|
| Title: Re: duplicate definitions with template member function |
18 Dec 2007 10:18:55 AM |
|
|
On 18 d=E9c, 16:37, "Victor Bazarov" <v.Abaza...@comAcast.net> 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
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: duplicate definitions with template member function |
18 Dec 2007 10:28:10 AM |
|
|
mathieu wrote:
On 18 déc, 16:37, "Victor Bazarov" <v.Abaza...@comAcast.net> 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
.
|
|
|
| User: "mathieu" |
|
| Title: Re: duplicate definitions with template member function |
18 Dec 2007 10:46:39 AM |
|
|
On 18 d=E9c, 17:28, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
mathieu wrote:
On 18 d=E9c, 16:37, "Victor Bazarov" <v.Abaza...@comAcast.net> 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
.
|
|
|
|
|
|
|

|
Related Articles |
|
|