Why the code below does not compile?
#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)>;
};
With GCC I get:
error: 'static constexpr uint64_t A::Helper::p2(uint8_t)' called in a constant expression before its definition is complete
My understanding was that p2 function should be defined because Helper class is fully compiled.
Tried MSVC Compiler Version 19.29.30040 for x86 and GCC 12.
EDIT1:
template and non-template classes behaves differently. For example the code below compiles:
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>;
Helper::p2(5)is actuallyA::Helper::p2(5), andAhas yet to be defined.