This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Initialization of a static member of a template class fails
- From: Valery Jean-Michel <Jean-Michel dot Valery at fr dot thalesgroup dot com>
- To: "'gcc-help at gcc dot gnu dot org'" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 26 Jul 2007 16:40:50 +0200
- Subject: Initialization of a static member of a template class fails
Hi,
I'm facing a problem with gcc and I don't know if it's a known bug or not.
I have found (<http://www.ddj.com/dept/cpp/184403955>) an example of a
template class to manage enum which have worked well with gcc 3.2.3 20030502
(Red Hat Linux 3.2.3-42) (after a few modifications to enable compiling).
But, as I'm using now gcc 3.4.5 20051201, I had to modify the initialization
of a static member (s_instances in the code below) to avoid compilation
errors but I get a segmentation fault when running the program.
Thanks by advance for your help.
Best regards.
Jean-Michel
Code of the template class:
#include <set>
template <class T>
class Enum {
private:
// Predicate for finding the corresponding instance
struct Enum_Predicate_Corresponds:
public std::unary_function<const Enum<T>*, bool> {
Enum_Predicate_Corresponds(int Value): m_value(Value) { }
bool operator()(const Enum<T>* E)
{ return E->Get_Value() == m_value; }
private:
const int m_value;
};
// Comparison functor for the set of instances
struct Enum_Ptr_Less:
public std::binary_function<const Enum<T>*, const Enum<T>*, bool> {
bool operator()(const Enum<T>* E_1, const Enum<T>* E_2)
{ return E_1->Get_Value() < E_2->Get_Value(); }
};
protected:
// Constructors
Enum(int Value): m_value(Value) {
s_instances.insert(this);
};
public:
// Compiler-generated copy constructor and operator= are OK.
typedef typename std::set<const Enum<T>*, Enum_Ptr_Less> instances_list;
typedef typename instances_list::const_iterator const_iterator;
// Access to int value
int Get_Value(void) const { return m_value; }
static int Min(void) { return (*s_instances.begin())->m_value; }
static int Max(void) { return (*s_instances.rbegin())->m_value; }
static const Enum<T>* Corresponding_Enum(int Value)
{ const_iterator it = find_if(s_instances.begin(), s_instances.end(),
Enum_Predicate_Corresponds(Value));
return (it != s_instances.end()) ? *it : NULL; }
static bool Is_Valid_Value(int Value) { return Corresponding_Enum(Value)
!= NULL; }
// Number of elements
//static instances_list::size_type size(void) { return s_instances.size();
static int size(void) { return s_instances.size();
}
// Iteration
static const_iterator begin(void) { return s_instances.begin(); }
static const_iterator end(void) { return s_instances.end(); }
private:
int m_value;
static instances_list s_instances;
};
Main code:
#include "Enum.h"
typedef enum _e_MODE {
MODE_OFF = 0,
MODE_ON = 1
} MODE;
class MODE_Enum: public Enum<MODE_Enum> {
private:
explicit MODE_Enum(int Value): Enum<MODE_Enum>(Value) { }
public:
static const MODE_Enum mode_on;
static const MODE_Enum mode_off;
};
template <class T> typename Enum<T>::instances_list Enum<T>::s_instances;
/* Previous code used with older gcc (3.2.3 20030502 (Red Hat Linux
3.2.3-42) )was
Enum<SONAR_MODE_Enum>::instances_list Enum<SONAR_MODE_Enum>::s_instances;
which leads to, with latest gcc (3.4.5 20051201 (Red Hat 3.4.5-2)):
error: too few template-parameter-lists
*/
const MODE_Enum MODE_Enum::mode_off(MODE_OFF);
const MODE_Enum MODE_Enum::mode_on(MODE_ON);
int main(int argc, char* argv[])
{
}