对静态 constexpr 成员的未定义引用仅由值使用
Undefined reference to static constexpr member only used by value
我试图创建一个包含字体样式的聪明类。在此之前,它由 3 个具有按位兼容值的枚举组成(每组值与其他枚举没有重叠位),因此您可以执行FontStyle::LEFT | FontStyle::TOP
但是 clang 警告我不要组合不相关的枚举,是的,我在这里看到了可能的错误:FontStyle::LEFT | FontStyle::RIGHT
确实设置了这两个位。因此,我使用以前的枚举和模板的帮助程序类重新设计了该类,以匹配正确的值。但是现在我在调试构建上的 clang 收到链接器错误,大约undefined reference
我的static constexpr
成员。
查看静态 constexpr 成员的未定义引用错误表明,该值是 ODR 使用的,但我没有使用任何引用。
静态 constexpr 类成员何时需要类外定义? 然后,这将我指向我的帮助程序类的隐式复制构造函数,这就是问题所在。
我是否有机会避免 C++14(C++17 已经允许省略它们)和调试构建(Ctors 在发布中进行了优化,因此没有未定义的引用)中的类外定义?
相关代码:
#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;
是 ODR 对FontStyle::CENTER
的使用。
我试过使用
constexpr FontStyle style = FontStyle::CENTER;
但我在构造函数中遇到了问题。以下内容有效,尽管我不清楚这是否可以满足您的需求。
int main() {
constexpr auto v = FontStyle::CENTER;
FontStyle style = v;
return 0;
}
这会将 ODR 使用的责任转移到constexpr auto v
.
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中