 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Wu Yongwei Guest
|
Posted: Wed May 03, 2006 3:06 pm Post subject: Multiple definition and specialization of static data member |
|
|
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
|
Posted: Wed May 03, 2006 7:06 pm Post subject: Re: Multiple definition and specialization of static data me |
|
|
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 |
|
 |
|
|
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
|
|