Re: Multiple definition and specialization of static data member
Maxim Yegorushkin wrote:
Wu Yongwei wrote:
I posted the following message days ago to comp.std.c++, and
got an helpful answer from Alberto Ganesh Barbati. However,
I would like to hear more comments from you gurus here, esp.
whether the behaviour of MSVC 8 is standard conforming, best
with quotations from the Standard.
-------------------------------
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<double>::Epsilon'
...
Static data members of class templates are an exempt from One
Definition Rule [3.2/5], this is why you can define the static
member in the header file without getting a multiple
definitions error.
OTOH, full specialization is not a template so that the exempt
does not apply here, this is why you get the error when you
put the full specialization of the static member in the
header.
Exactly. On the other hand, the explicit specialization must be
declared in the header in order to prevent an implicit
specialization. How do you declare the explicit specialization
without defining the variable?
The only thing I could come up with is:
template<>
extern double FMC< double >::Epsilon = ...
I'm not 100% sure that extern is legal here, although I cannot
find anything which forbids it. And I'm almost sure that I've
encountered a compiler which refuses it.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]