Static const member initialization in templated class
Asked Answered
T

1

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.

Troxell answered 22/7, 2010 at 8:31 Comment(4)
You are missing the 'const' modifier in the definition.Reverie
Are you sure this is your code? You shouldn't get any errors. @Ropez: The const only matters in the declaration.Person
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.Pearlpearla
Possible duplicate of How to define a static const variable of a template classDiablerie
P
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.

Pawn answered 22/7, 2010 at 8:33 Comment(4)
I think that your point is wrong. Look at https://mcmap.net/q/301131/-template-static-variableTearing
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.Algonkian
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.Tearing
for the sake of completeness you should also show how to define the static member when it is member of a template.Diablerie

© 2022 - 2024 — McMap. All rights reserved.