我发现了一些C++11代码,看起来像这样:
// some_file.h
namespace blah {
class X {
public:
constexpr const static std::initializer_list<uint64> SOME_LIST =
{1,2,3};
};
}
// some_file.cpp
#include "some_file.h"
namespace blah {
constexpr const std::initializer_list<uint64> X::SOME_LIST;
}
这编译起来很顺利。我认为在cpp文件中定义是为了避免每个包含头文件的文件中都出现符号重复(如有错误,请指正)。
然后我尝试了以下操作:
// my_file.h
namespace bleh {
constexpr const static char SOME_CONSTANT[] = "yay";
}
// my_file.cpp
#include "my_file.h"
namespace bleh {
// if I add this or any other variation, compilation breaks!
//constexpr const static char SOME_CONSTANT[];
}
如果我在.cpp文件中添加显式定义,则上述代码将无法工作。因此,我想知道:第二种情况是否存在符号重复?如果有,是否有一种方法可以定义不带封闭类的变量?
constexpr const X::SOME_LIST;
缺少类型说明符。您确定这是编译的代码吗? - typ1232