[英]Undefined reference to static constexpr member only used by value
我试图创建一个包含字体样式的聪明类。 在此之前,它由3个枚举组成,这些枚举具有按位兼容的值(每个值集与其他枚举没有重叠的位),因此可以执行FontStyle::LEFT | FontStyle::TOP
FontStyle::LEFT | FontStyle::TOP
但是clang警告我不要合并无关的枚举,是的,我在这里看到了可能的错误: FontStyle::LEFT | FontStyle::RIGHT
FontStyle::LEFT | FontStyle::RIGHT
确实设置了两个位。 因此,我对以前的枚举和模板使用了一个帮助器类来对该类进行重做,以匹配正确的值。 但是现在我在Debug版本中收到有关clang的链接器错误,该错误是关于对我的static constexpr
成员的undefined reference
。
查看静态constexpr成员的未定义引用错误表明,该值是ODR使用的,但是我没有使用任何引用。
静态constexpr类成员何时需要类外定义? 然后,这使我指向了帮助程序类的隐式副本构造函数,这就是问题所在。
我有没有机会避免C ++ 14中的类定义(C ++ 17已经允许忽略它们)和Debug构建(Ctor在Release中得到了优化,因此没有未定义的引用)?
相关代码 :
#include <array>
#include <cstdint>
namespace detail {
template<unsigned T_index>
struct FontStylePart
{
constexpr FontStylePart(uint8_t val) : value(val) {}
uint8_t value;
};
} // namespace detail
class FontStyle
{
static constexpr unsigned AlignH = 0;
static constexpr unsigned AlignV = 1;
public:
constexpr FontStyle() = default;
template<unsigned T_index>
constexpr FontStyle(detail::FontStylePart<T_index> style) : FontStyle()
{
value[T_index] = style.value;
}
/// Horizontal align
static constexpr detail::FontStylePart<AlignH> LEFT = 0;
static constexpr detail::FontStylePart<AlignH> RIGHT = 1;
static constexpr detail::FontStylePart<AlignH> CENTER = 2;
/// Vertical align
static constexpr detail::FontStylePart<AlignV> TOP = 0;
static constexpr detail::FontStylePart<AlignV> BOTTOM = 1;
static constexpr detail::FontStylePart<AlignV> VCENTER = 2;
private:
std::array<uint8_t, 3> value = {{0, 0, 0}};
};
int main() {
FontStyle style = FontStyle::CENTER;
return 0;
}
线
FontStyle style = FontStyle::CENTER;
是FontStyle::CENTER
的ODR使用。
我尝试使用
constexpr FontStyle style = FontStyle::CENTER;
但是我在构造函数中遇到了问题。 尽管尚不清楚我是否能满足您的需求,但以下方法可以工作。
int main() {
constexpr auto v = FontStyle::CENTER;
FontStyle style = v;
return 0;
}
这会将ODR使用的责任转移给constexpr auto v
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.