C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Multiple definition and specialization of static data member

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Wu Yongwei
Guest





PostPosted: Wed May 03, 2006 3:06 pm    Post subject: Multiple definition and specialization of static data member Reply with quote



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 ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Wed May 03, 2006 7:06 pm    Post subject: Re: Multiple definition and specialization of static data me Reply with quote



Wu Yongwei ha scritto:
Quote:
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...

Quote:

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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.