constexpr

Simple constexpr LookUpTable in C++14

北战南征 提交于 2019-12-05 03:03:43
I am trying to make a simple LookUpTable based on an array of integers, where the idea is to have it calculated at compile time . Trying to make it possible to use it for any other future tables of various integer types I might have, I need it as a template . So I have a LookUpTable.h #ifndef LOOKUPTABLE_H #define LOOKUPTABLE_H #include <stdexcept> // out_of_range template <typename T, std::size_t NUMBER_OF_ELEMENTS> class LookUpTableIndexed { private: //constexpr static std::size_t NUMBER_OF_ELEMENTS = N; // LookUpTable T m_lut[ NUMBER_OF_ELEMENTS ] {}; // ESSENTIAL T Default Constructor for

Constexpr variable evaluation

删除回忆录丶 提交于 2019-12-05 02:57:03
问题 Here is my code and I need clarification on what's happening: constexpr int funct(int x){ return x + 1; } int main(){ int x = funct(10); return 0; } constexpr 's allows compile time calculation, and based on my code above, since funct is declared as constexpr , it is allowed to do compile time calculations if the arguments are constants or constexpr's themselves. The part that I am confused with lies in this part, the int x . Since it is not declared as constexpr , would it mean that int x

Compute nth prime at compile time [closed]

匆匆过客 提交于 2019-12-05 01:02:55
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . The C++11 features, with constexpr and template argument packs, should in my opinion be strong enough to perform some rather complex computations. One

Changing active member of union in constant expressions

自闭症网瘾萝莉.ら 提交于 2019-12-05 01:00:48
问题 Playing with constexpr and union I found out, that I can't change active member of an union in constexpr . Just one exception: union of empty classes. constexpr bool t() { struct A {}; struct B {}; union U { A a; B b; } u{}; u.a = A{}; u.b = B{}; return true; } static_assert(t()); constexpr bool f() { struct A { char c; }; struct B { char c; }; union U { A a; B b; } u{}; u.a = A{}; u.b = B{}; // error originating from here return true; } static_assert(f()); First function may produce constant

constexpr, arrays and initialization

吃可爱长大的小学妹 提交于 2019-12-05 00:51:45
问题 Is there anything in the world of C++ that would make what I'm trying to do possible? template < typename T , size_t Size > struct array { constexpr T buf[Size]; constexpr size_t size() const { return Size; } }; template < typename T , size_t Size > constexpr array<T,Size+1> push_back(array<T,Size> const& arr, T const& val) { array<T,Size+1> arr_out = {{arr.buf, val}}; return arr_out; } What I'm trying to do is create a new array initialized with the data in the other, and put a new element

constexpr with string operations workaround?

Deadly 提交于 2019-12-05 00:51:36
问题 This previously answered question explains why the code I have posted below does not work. I have a follow-up question: is there a workaround that is conceptually equivalent, i.e., achieves compile-time string concatenation, but is implemented in a way that is actually supported by C++11? Using std::string is completely non-essential. constexpr std::string foo() { return std::string("foo"); } constexpr std::string bar() { return std::string("bar"); } constexpr std::string foobar() { return

Is a constexpr more “constant” than const?

梦想的初衷 提交于 2019-12-05 00:41:09
The C++ Programming Language Fourth Edition - Bjarne Stroustrup: (emphasis mine) 2.2.3. Constants In a few places, constant expressions are required by language rules (e.g., array bounds (§2.2.5, §7.3), case labels (§2.2.4, §9.4.2), some template arguments (§25.2), and constants declared using constexpr). In other cases, compile-time evaluation is important for performance. Independently of performance issues, the notion of immutability (of an object with an unchangeable state) is an important design concern (§10.4). It seems that Stroustrup is suggesting here that constexpr ensures

Proper initialization of static constexpr array in class template?

不问归期 提交于 2019-12-05 00:32:12
Static class members in C++ have caused a little confusion for me due to the standard's verbiage: 9.4.2 Static data members [class.static.data] The declaration of a static data member in its class definition is not a definition... However a constexpr is required to be initialized (AFAIK, couldn't find a quote from the standard) at its declaration (e.g., in the class definition). Because of the restrictions on constexpr I had actually forgotten about the requisite for static members to be defined outside of the class, until I tried accessing a static constexpr array. This related question

In C++11 is sqrt defined as constexpr?

旧时模样 提交于 2019-12-04 23:37:41
In C++11 is std::sqrt defined as constexpr , i.e. can it legally be used from other constexpr functions or in compile-time contexts like array sizes or template arguments? g++ seems to allow it (using -std=c++0x ), but I'm not sure I can take that as authoritative given that c++0x/c++11 support is still incomplete. The fact that I can't seem to find anything on the Internet makes me unsure. It seems like this should be something one could easily find out using Google, but I've tried (for 40 minutes now...) and couldn't find anything. I could find several proposals for adding constexpr to

C++1y/C++14: Assignment to object outside its lifetime is not allowed in a constant expression?

青春壹個敷衍的年華 提交于 2019-12-04 23:02:19
Is the following C++14/C++1y program ill-formed according to the current draft? #include <cstddef> template<typename T, size_t n> struct literal_array { T data[n]; }; template<typename T, size_t n, size_t m> constexpr literal_array<T, n+m> operator+(literal_array<T, n> a, literal_array<T, m> b) { literal_array<T, n+m> x; for (size_t i = 0; i < n; i++) x.data[i] = a.data[i]; for (size_t i = 0; i < m; i++) x.data[n+i] = b.data[i]; return x; } int main() { constexpr literal_array<int, 3> a = { 1, 2, 3 }; constexpr literal_array<int, 2> b = { 4, 5 }; constexpr auto c = a + b; } Clang trunk (at