简体   繁体   English

如何初始化模板方法中使用的静态类成员?

[英]How to initialize static class member used in template method?

I want a static constant, LIST_DELIMITER , defined in my class below. 我想要在下面的类中定义的静态常量LIST_DELIMITER 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. 我知道在stackoverflow上也有类似的问题,但是我似乎找不到我想要的东西。 One thing that is different in my case is that my whole class is not templated, just the single method. 与我的情况不同的一件事是,我的整个类不是模板化的,而仅仅是单个方法。

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. 此静态类成员定义中没有模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM