简体   繁体   中英

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 .

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> {
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM