2015-04-26 2 views
6

Я уменьшил мою проблему к следующему:г ++ жалуется constexpr функция не является постоянным выражением

struct A { 
    static constexpr std::size_t f() { return 4; } 
}; 

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

Я не понимаю, что случилось с этим, но если я пытаюсь скомпилировать с помощью 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 

Воспроизведение доступно on coliru.

+2

(http://comments.gmane.org/gmane.comp.gcc. bugs/425219) GCC 5.1 дает мне ICE. – chris

+6

[Сюжет сгущается ...] (http://coliru.stacked-crooked.com/a/3e4e9d42d7ee440a) – orlp

ответ

0

Я не знаю, почему исходный код плох, но здесь обходной путь: [? Это, возможно]

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> { 
}; 
Смежные вопросы