G ++ complains that constexpr function is not a constant expression

I reduced my problem to the following:

struct A { static constexpr std::size_t f() { return 4; } }; template<std::size_t N> struct B : A { alignas(A::f()) char a[N]; }; 

I do not see what is wrong with this, but if I try to compile using g++ :

 main.cpp:9:19: error: expression 'A::f' is not a constant-expression alignas(A::f()) char a[N]; ^ main.cpp:9: confused by earlier errors, bailing out 

Playback is available on coliru .

+6
source share
1 answer

I don't know why the source code is bad, but here is a workaround:

 struct A { static constexpr std::size_t f() { return 4; } }; template<std::size_t ALIGN, std::size_t N> struct C { alignas(ALIGN) char a[N]; }; template<std::size_t N> struct B : A, C<A::f(), N> { }; 
0
source

Source: https://habr.com/ru/post/985998/


All Articles