Search code examples
c++templatesmemberstatic-members

How to initialize static class member used in template method?


I want a static constant, LIST_DELIMITER, defined in my class below. However, I can't figure out how to declare it with templates.

// MyClass.h
#pragma once
#include <boost/algorithm/string.hpp>
#include <vector>

class MyClass
{
public:
    MyClass();
    virtual ~MyClass();

    template<class T>
    void GetAsVectorOfValues(std::vector<T> values)
    {
        boost::split(values, value_, boost::is_any_of(LIST_DELIMITER));
    }

private:
    std::string value_;
    static const std::string LIST_DELIMITER;
};

// MyClass.cpp
std::string MyClass::LIST_DELIMITER = ",";

I know there are similar question on stackoverflow but I can't seem to find what I'm looking for. One thing that is different in my case is that my whole class is not templated, just the single method.


Solution

  • You have to use the exact same declaration, including qualifiers:

    const std::string MyClass::LIST_DELIMITER = ",";
    ^^^^^
    

    There's no template involved in this static class member definition.