Topic: Multiple definition and specialization of static data member


Author: "Wu Yongwei" <wuyongwei@gmail.com>
Date: 3 May 2006 15:00:19 GMT
Raw View
I encountered a problem in a header file like the following:

template <typename DataType>
class FMC
{
public:
    static DataType Epsilon;
private:
    FMC() {}
};

template <typename DataType>
DataType FMC<DataType>::Epsilon = static_cast<DataType>(0.000001);

template <>
double FMC<double>::Epsilon = static_cast<double>(0.0000000001);

When multiple .cpp files include this header file, GCC 3.4.4 (or some
earlier version) will complain:

.: multiple definition of `FMC<float>::Epsilon'
.

To make GCC work, I have to reorganize the header file as follows:

- In header file

template <typename DataType>
class FMC
{
public:
    static DataType Epsilon;
private:
    FMC() {}
};

template <typename DataType>
DataType FMC<DataType>::Epsilon = static_cast<DataType>(0.000001);

template <>
double FMC<double>::Epsilon;

- In another .cpp file

#include "fmc.h"

template <>
double FMC<double>::Epsilon = static_cast<double>(0.0000000001);

However, then MSVC 8 will choke and declare that the definition in the
.cpp file is a redefinition.

My question is, which way is the standard-conforming way? and which
compiler is standard conformant on this issue?

Best regards,

Yongwei

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Wed, 3 May 2006 18:33:42 GMT
Raw View
Wu Yongwei ha scritto:
> I encountered a problem in a header file like the following:
>
> template <typename DataType>
> class FMC
> {
> public:
>     static DataType Epsilon;
> private:
>     FMC() {}
> };
>
> template <typename DataType>
> DataType FMC<DataType>::Epsilon = static_cast<DataType>(0.000001);
>
> template <>
> double FMC<double>::Epsilon = static_cast<double>(0.0000000001);
>
> When multiple .cpp files include this header file, GCC 3.4.4 (or some
> earlier version) will complain:
>
> .: multiple definition of `FMC<float>::Epsilon'

float? you mean double, I presume...

>
> To make GCC work, I have to reorganize the header file as follows:
>
> <snip: moving FMC<double>::Epsilon definition into cpp file>
>
> However, then MSVC 8 will choke and declare that the definition in the
> .cpp file is a redefinition.
>
> My question is, which way is the standard-conforming way? and which
> compiler is standard conformant on this issue?
>

Looks like a bug in MSVC 8 (and 7.1) to me. The code you posted seems ok
to me and according to my interpretation is the only conformant way to
achieve what you want: you declared the explicit specialization of
FMC<double>::Epsilon in the header (so to forbid implicit instantiation
from the master template) and you defined it in the cpp file.
FYI the code compiles correctly on Comeau Online and gcc 4.0.2.

Ganesh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]