Why wouldn't this be valid for a static assert?

SOLVED

This is a stripped down version of what I'm doing. It gives the same error as the real one.

constexpr uint32_t AFlag = 0x01;
struct AStruct 
{
    const uint32_t m_AMember;
};
const AStruct Xyz
{
    0x03
};
static_assert((Xyz.m_AMember & AFlag) != 0);

It seems like everything in there could be evaluated at compile time, since it's all const and constexpr. But Visual C++ complains that it didn't evaluate to a constant.

It's obviously related to it being a structure member. Does static_assert just not work on structure members even in this simple type of scenario in 17?

Posted May 20, 2020, 2:54 PM UTC

13
Xeverous•

GCC:

main.cpp: In function 'int main()':
main.cpp:15:39: error: non-constant condition for static assertion
   15 | static_assert((Xyz.m_AMember & AFlag) != 0);
	  |               ~~~~~~~~~~~~~~~~~~~~~~~~^~~~
main.cpp:15:39: error: the value of 'Xyz' is not usable in a constant expression
main.cpp:11:15: note: 'Xyz' was not declared 'constexpr'
   11 | const AStruct Xyz
	  |               ^~~

constexpr AStruct Xyz fixes the problem.

2
Full-SpectralOP•

Ahh... You learn something every day. I wasn't even aware after all this time that structs could be constexpr. Thanks.

1
Xeverous•

The type can not be constexpr, but each instance can be.

1