Having written a simple compilation time std::array factory from a generator function, I came across this: clang ++ 3.5.1 and g ++ 4.9.2 do not agree whether the function is constexpr or not.
Code (this is C ++ 14!):
#include <array> #include <utility> template <class T, std::size_t N, class GenType, std::size_t... I> constexpr std::array<T, N> make_array_impl (GenType gen, std::index_sequence <I...>) { return {{ gen (I)... }}; } template <class T, std::size_t N, class GenType> constexpr std::array<T, N> make_array (GenType gen) { return make_array_impl <T, N> ( gen, std::make_index_sequence <N> {} ); } constexpr int generator_const (std::size_t /* index */) { return 1; } constexpr auto a = make_array <int, 3> (generator_const); static_assert (a.size () == 3, ""); static_assert (a[0] == 1, ""); static_assert (a[1] == 1, ""); static_assert (a[2] == 1, ""); int main () {}
Compiling with g ++:
migou ~ % g++ -std=c++14 ex.cpp ex.cpp:28:41: in constexpr expansion of 'make_array<int, 3ul, int (*)(long unsigned int)>(generator_const)' ex.cpp:18:5: in constexpr expansion of 'make_array_impl<int, 3ul, int (*)(long unsigned int), {0ul, 1ul, 2ul}>(gen, (std::make_index_sequence<3ul>{}, std::make_index_sequence<3ul>()))' ex.cpp:8:21: error: expression 'generator_const' does not designate a constexpr function return {{ gen (I)... }};
With clang ++, it compiles just fine. Can I continue to review this valid g ++ 14 (and therefore g ++ listening)?
source share