[英]Initialize a static constexpr member in templated class
我很惊讶没有在这里找到我的问题的答案。 如果我忽略了,可以随时将我指向此处。
编辑:我收到一条通知,说这可能是为什么模板只能在头文件中实现的可能重复? 尽管其中包含许多有关编译器如何处理类模板化类的有用信息,但我仍然找不到有关如何处理类所需的静态常量成员的信息,而这些成员对于所有可能的模板实例化实际上只需要一次。
我的用例是将模板化的浮点数转换为字符串的转换类。 一个成员函数应该是创建带有si前缀的数字的函数。 因此,需要一些带有前缀字符的查找数组-该数组与实际选择的任何模板浮点类型无关。 我做了这样的事情:
// Float2String.h
#include <string>
template <typename FloatType>
class Float2String
{
public:
//...
static std::string withSIPrefix (FloatType floatNumber, int numSignificantDigits)
{
// scale the number, convert it to a string and compute the idx to pick the right prefix...
return floatNumberScaledAndConvertedToString + siPrefixes[idx];
}
private:
static constexpr char[11][3] siPrefixes = {"f", "p", "n", "μ", "m", "", "k", "M", "G", "T", "P"};
};
// Float2String.cpp
#include "Float2String.h"
template <typename FloatType>
constexpr char Float2String<FloatType>::siPrefixes[11][3];
当尝试实际使用它(例如转换双精度数)时,出现以下链接器错误:
Error:Undefined symbol 'Float2String<double>::siPrefixes' referenced from:
Error: Float2String<double>::withSIPrefix(double, int) in Main.o
我在使用Clang的Mac OS上,并启用了C ++ 14进行编译。
问题:我该怎么做? 也许可以通过其他方法更好地做到这一点吗?
也许您可以在模板校准之外定义siPrefixes。 它不取决于您的模板类型。
您可以直接在cpp文件中定义它,并在cpp文件中使用SIPrefix实现移动方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.