在使用boost :: assign :: list_of时声明时,不能通过非常量表达式初始化

时间:2017-09-22 13:57:59

标签: c++ boost c++98

我正在尝试使用boost::assign::list_of()在类中声明静态集。

MyClass.h

class MyClass
{
    public:
        static std::set<std::string> & formats_set();
    private:
        static const std::set<std::string> formats_;
}

MyClass.cpp

const std::set<std::string> MyClass::formats_ = boost::assign::list_of(
    "Format1"
   ,"Format2"
   ,"Format3");

然而 - 当我尝试编译时,我收到错误 ‘MyClass::formats_’ cannot be initialized by a non-constant expression when being declared

有没有办法解决这个问题? 谢谢!

1 个答案:

答案 0 :(得分:1)

现在让我们用正确的语法尝试:

#include <string>
#include <set>
#include <boost/assign/list_of.hpp> // for 'list_of()'

class MyClass
{
    public:
        static std::set<std::string> & formats_set();
    private:
        static const std::set<std::string> formats_;
};

const std::set<std::string> MyClass::formats_ = boost::assign::list_of
   ("Format1")
   ("Format2")
   ("Format3");