Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ complains constexpr function is not a constant expression

I've 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 don't see what's wrong with this, yet 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

Reproduction is available on coliru.

like image 442
orlp Avatar asked Apr 26 '15 15:04

orlp


1 Answers

I don't know why the original 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> {
};
like image 72
Goswin von Brederlow Avatar answered Oct 04 '22 10:10

Goswin von Brederlow