C++11统一初始化:字段初始值设定项不是常量
C++11 uniform initialization: Field initializer is not constant
我正在尝试像这样实例化一组字符串:
class POI {
public:
...
static const std::set<std::string> TYPES { "restaurant", "education", "financial", "health", "culture", "other" };
...
}
现在,当我这样做时,我得到了这些错误(全部在这一行):
error: field initializer is not constant
static const std::set<std::string> TYPES { "restaurant", "education", "financial", "health", "culture", "other" };
error: in-class initialization of static data member 'const std::set<std::basic_string<char> > POI::TYPES' of non-literal type
error: non-constant in-class initialization invalid for static member 'POI::TYPES'
error: (an out of class initialization is required)
error: 'POI::TYPES' cannot be initialized by a non-constant expression when being declared
如果我假设集合中的字符串不被视为常量,那对我来说是有意义的。这真的是这里的问题吗?不幸的是,我找不到将初始化程序中的那些字符串声明为 const 的方法。这可能吗?
您必须离线初始化您的静态变量,如:
#include <set>
#include <string>
class POI {
public:
static const std::set<std::string> TYPES;
};
const std::set<std::string> POI::TYPES { "restaurant", "education", "financial", "health", "culture", "other" };
这适用于整数/枚举类型,如标准(第 9.4.2 节:)所指定
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression. In that case, the member can appear in integral constant expressions within its scope.
C++ 中的初始化器是定义的一部分,而不是声明的一部分。 const
整数和 enum
类型放宽了这一点。在 C++11 中,为 文字类型
的 constexpr
成员添加了进一步的指令
[class.static.data]/p3
If a non-volatile const static data member is of integral or enumeration type, its declaration in the class
definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression
is a constant expression (5.20). A static data member of literal type can be declared in the
class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer
in which every initializer-clause that is an assignment-expression is a constant expression. [ ... ] The member shall still be defined
in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not
contain an initializer
由于它不适用于您的情况,因此您应该离线初始化您的静态变量as shown in this example
class POI {
public:
static const std::set<std::string> TYPES;
};
const std::set<std::string> POI::TYPES = {
"restaurant", "education", "financial", "health", "culture", "other"
};
我正在尝试像这样实例化一组字符串:
class POI {
public:
...
static const std::set<std::string> TYPES { "restaurant", "education", "financial", "health", "culture", "other" };
...
}
现在,当我这样做时,我得到了这些错误(全部在这一行):
error: field initializer is not constant
static const std::set<std::string> TYPES { "restaurant", "education", "financial", "health", "culture", "other" };
error: in-class initialization of static data member 'const std::set<std::basic_string<char> > POI::TYPES' of non-literal type
error: non-constant in-class initialization invalid for static member 'POI::TYPES'
error: (an out of class initialization is required)
error: 'POI::TYPES' cannot be initialized by a non-constant expression when being declared
如果我假设集合中的字符串不被视为常量,那对我来说是有意义的。这真的是这里的问题吗?不幸的是,我找不到将初始化程序中的那些字符串声明为 const 的方法。这可能吗?
您必须离线初始化您的静态变量,如:
#include <set>
#include <string>
class POI {
public:
static const std::set<std::string> TYPES;
};
const std::set<std::string> POI::TYPES { "restaurant", "education", "financial", "health", "culture", "other" };
这适用于整数/枚举类型,如标准(第 9.4.2 节:)所指定
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression. In that case, the member can appear in integral constant expressions within its scope.
C++ 中的初始化器是定义的一部分,而不是声明的一部分。 const
整数和 enum
类型放宽了这一点。在 C++11 中,为 文字类型
constexpr
成员添加了进一步的指令
[class.static.data]/p3
If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.20). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ ... ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer
由于它不适用于您的情况,因此您应该离线初始化您的静态变量as shown in this example
class POI {
public:
static const std::set<std::string> TYPES;
};
const std::set<std::string> POI::TYPES = {
"restaurant", "education", "financial", "health", "culture", "other"
};