views:

162

answers:

1

I have a problem regarding 'static const' member initialization. In a templated class I define a const member and initialize it outside the class.
When I include the .h file where this class is implemented in multiple .cpp files, I get an LNK2005 error (I'm using VS2010) that says the constant is already defined.

// List.hpp
template <class T>
class List {
    static const double TRIM_THRESHOLD;
};

template <class T>
const double List<T>::TRIM_THRESHOLD = 0.8;

I tried putting the member initialization in a .cpp file, but then I get a linker error saying that the constant is not defined at all. If the list is not templated and I put the initialization in a .cpp file, all is fine.
Is there any solution for this situation? I have #ifdef/define clauses around the file already, and it's definitely not a solution.

+1  A: 

You should define the constant in a source file not a header (so it only gets defined once) since this is a template which you need to keep in the header(and all instances have the same value) you can use a common base class.

class ListBase {
protected:
    ListBase() {} // use only as base 
    ~ListBase() { } // prevent deletion from outside
    static const double TRIM_THRESHOLD;    
};

template <class T>
class List : ListBase {  
};

// in source file
double ListBase::TRIM_THRESHOLD = 0.8;

Another option is to have it as a static function:

    static double trim_threashold() { return 0.8; }
Motti
I think that your point is wrong. Look at http://stackoverflow.com/questions/1553854/template-static-variable
vnm
i don't think he is wrong, because he is used inheritance, where the static member is in the ancestor which is not the templated one. then using static function is valid too.
uray
If we need same constant value for each of instantiations of template (TRIM_THRESHOLD is same for List<int> or List<float>) Motti's solution is right. If we need to do this just for escaping of linker errors (multiple definitons of same simbol), this solution is overkill. Language allows us define static variable of template class in header. If Motti meant first case - I appologize, because I didn't understand his point.
vnm