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

cannot be initialized by a non-constant expression when being declared when using boost::assign::list_of

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

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

有什么办法可以解决这个问题吗? 谢谢!

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

#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");