7

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.

Gratian Lup
  • 1,485
  • 3
  • 19
  • 29
  • 1
    You are missing the 'const' modifier in the definition. – Ropez Jul 22 '10 at 08:34
  • 1
    Are you sure this is your code? You shouldn't get any errors. @Ropez: The `const` only matters in the declaration. – GManNickG Jul 22 '10 at 08:37
  • Interestingly VS2015 does not have this problem. It somehow can optimize/figure out that the static const definitions are really all the same thing and does not flag an error in the linker. – Menace May 09 '16 at 01:06
  • Possible duplicate of [How to define a static const variable of a template class](https://stackoverflow.com/questions/34026555/how-to-define-a-static-const-variable-of-a-template-class) – 463035818_is_not_an_ai Feb 12 '18 at 13:03

1 Answers1

9

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; }

Edit: If your compiler supports C++11 you make your static method a constexpr function so that it has all the optimization opportunities that using the value directly has.

Motti
  • 110,860
  • 49
  • 189
  • 262
  • I think that your point is wrong. Look at http://stackoverflow.com/questions/1553854/template-static-variable – cybevnm Jul 22 '10 at 09:13
  • 1
    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 Jul 22 '10 at 11:25
  • If we need same constant value for each of instantiations of template (TRIM_THRESHOLD is same for List or List) 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. – cybevnm Jul 22 '10 at 12:06
  • 1
    for the sake of completeness you should also show how to define the static member when it **is** member of a template. – 463035818_is_not_an_ai Feb 12 '18 at 13:05