Why isn’t a for-loop a compile-time expression?

Here’s a way to do it that does not need too much boilerplate, inspired from http://stackoverflow.com/a/26902803/1495627 : template<std::size_t N> struct num { static const constexpr auto value = N; }; template <class F, std::size_t… Is> void for_(F func, std::index_sequence<Is…>) { using expander = int[]; (void)expander{0, ((void)func(num<Is>{}), 0)…}; } template <std::size_t N, typename F> void for_(F … Read more

Can I obtain C++ type names in a constexpr way?

Well, you could, sort of, but probably not quite portable: struct string_view { char const* data; std::size_t size; }; inline std::ostream& operator<<(std::ostream& o, string_view const& s) { return o.write(s.data, s.size); } template<class T> constexpr string_view get_name() { char const* p = __PRETTY_FUNCTION__; while (*p++ != ‘=’); for (; *p == ‘ ‘; ++p); char const* … Read more

Getting around the reinterpret cast limitation with constexpr

I can’t see how a reinterpret cast in this or similar cases can be any different from arithmetic operators It isn’t portable. You are probably aware of the fact that your code causes undefined behavior, since you dereference a type punned pointer and thus break strict aliasing. Moreover, since C++14, operations that would invoke undefined … Read more

inline vs. constexpr?

Asserting that something can be computed at compile-time is a pretty strong kind of optimization. Inlining merely removes a function call, by copy/pasting the function body into the call site. The function body still has to be executed, you just save the overhead of a function call. But if you make the same code be … Read more

GCC accepts `constexpr struct {} s;` but Clang rejects it. Who is correct?

Clang adheres to the following passage in [dcl.init]/7: If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor. This wording is defective and hence ignored by GCC (and, as of v3.9, also by Clang). The above quote differs … Read more

Is constexpr-based computation Turing complete?

tl;dr: constexpr in C++11 was not Turing-complete, due to a bug in the specification of the language, but that bug has been addressed in later drafts of the standard, and clang already implements the fix. constexpr, as specified in the ISO C++11 international standard, is not Turing-complete. Sketch proof: Every constexpr function f‘s result (or … Read more

Get min / max value of a static constexpr array at compile time

Let’s get the C++17 solution out of the way for future search-landers: constexpr int IntArray[] = {1, 5, 10, 12, 17}; constexpr int min = *std::min_element(std::begin(IntArray), std::end(IntArray)); static_assert(min == 1); C++11 is more picky with constexpr functions, so we have to roll out a recursive algorithm. This one is a simple, linear one: template <class … Read more

How can I get the depth of a multidimensional std::vector at compile time?

A classic templating problem. Here’s a simple solution like how the C++ standard library does. The basic idea is to have a recursive template that will count one by one each dimension, with a base case of 0 for any type that is not a vector. #include <vector> #include <type_traits> template<typename T> struct dimensions : … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)