constexpr函数调用无法编译
为什么下面的代码不能编译?
#include <stdint.h>
#include <array>
class A
{
struct Helper
{
static constexpr uint64_t p2(uint8_t n)
{
return static_cast<uint64_t>(1) << n;
}
};
using DenomArray = std::array<uint64_t, Helper::p2(5)>;
};
使用 GCC 我得到:
error: 'static constexpr uint64_t A::Helper::p2(uint8_t)' called in a constant expression before its definition is complete
我的理解是p2应该定义函数,因为Helper类是完全编译的。
为 x86 和 GCC 12 尝试了 MSVC 编译器版本 19.29.30040。
编辑1:
模板和非模板类的行为不同。例如下面的代码编译:
template <class T>
class A
{
private:
struct Helper
{
static constexpr T p2(uint8_t n)
{
return static_cast<T>(1) << n;
}
};
using DenomArray = std::array<T, Helper::p2(5)>;
};
using IntA = A<uint64_t>;