constexpr initialization of array to sort contents

It’s ugly, and probably not the best way to sort in a constant expression (because of the required instantiation depth).. but voilà, a merge sort: Helper type, returnable array type with constexpr element access: #include <cstddef> #include <iterator> #include <type_traits> template<class T, std::size_t N> struct c_array { T arr[N]; constexpr T const& operator[](std::size_t p) const … Read more

Why can I call a non-constexpr function inside a constexpr function?

The program is ill-formed and requires no diagnostic according to the C++11 draft standard section 7.1.5 The constexpr specifier paragraph 5 which says: For a constexpr function, if no function argument values exist such that the function invocation substitution would produce a constant expression (5.19), the program is ill-formed; no diagnostic required. and provides the … Read more

`static constexpr` function called in a constant expression is…an error?

As T.C. demonstrated with some links in a comment, the standard is not quite clear on this; a similar problem arises with trailing return types using decltype(memberfunction()). The central problem is that class members are generally not considered to be declared until after the class in which they’re declared is complete. Thus, regardless of the … Read more

Why is this constexpr static member function not seen as constexpr when called?

From memory, member function bodies are evaluated only once the class has been completely defined. static constexpr int bah = static_n_items(); forms part of the class definition, but it’s referring to a (static) member function, which cannot yet be defined. Solution: defer constant expressions to a base class and derive from it. e.g.: struct Item_id_base … Read more

Throw in constexpr function

clang is correct, note the HEAD revision of gcc accepts also accepts this code. This is a well-formed constexpr function, as long as there is value for the argument(s) that allows the function to be evaluated as a core constant expression. In your case 1 is such a value. This is covered in the draft … Read more