constexpr

c++11 constexpr flatten list of std::array into array

不想你离开。 提交于 2019-11-27 18:24:15
I am beginning with c++11, constexpr and template metaprogramming seems a nice way to save scarce ram on tiny microcontroler. Is there a way to write a template to flatten a list of constexpr array, what I need is a way to do : constexpr std::array<int, 3> a1 = {1,2,3}; constexpr std::array<int, 2> a2 = {4,5}; constexpr auto a3 = make_flattened_array (a1,a2); I use gcc 4.8.4 (arm-none-eabi), and can compile with std=c++11 or c++1y option if it is needed. Marco A. Notice - I understood your question as follows: you want to join those two arrays and flatten the result into a single, new array

What does it mean to “poison a function” in C++?

一笑奈何 提交于 2019-11-27 17:28:46
At the very end of Scott Schurr's talk "Introducing constexpr " at CppCon , he asks "Is there a way to poison a function"? He then explains that this can be done (albeit in a non-standard way) by: Putting a throw in a constexpr function Declaring an unresolved extern const char* Referencing the unresolved extern in the throw I sense that I'm a bit out of my depth here, but I'm curious: What does it mean to "poison a function"? What is the significance/usefulness of the technique he outlines? In general it refers to making a function unusable, e.g. if you want to ban the use of dynamic

Is it possible to know when is constexpr really a constexpr?

喜你入骨 提交于 2019-11-27 17:20:16
问题 Since the extended versions of constexpr (I think from C++14) you can declare constexpr functions that could be used as "real" constexpr, that is, the code executed at compile time or can behave as inline functions. So when can have this program: #include <iostream> constexpr int foo(const int s) { return s + 4; } int main() { std::cout << foo(3) << std::endl; const int bar = 3; std::cout << foo(bar) << std::endl; constexpr int a = 3; std::cout << foo(a) << std::endl; return 0; } Of course,

Static templated constexpr nested class member

元气小坏坏 提交于 2019-11-27 16:04:33
问题 I have the following sample class Foo with nested class Bar and everything is constexpr : class Foo { private: template <typename T> struct Bar { constexpr Bar(){} constexpr int DoTheThing() const { return 1; } }; public: constexpr static auto b = Bar<int>{}; constexpr Foo() {} constexpr int DoTheThing() const { return b.DoTheThing(); } }; And I want to test that calling Foo::DoTheThing returns 1: int main() { constexpr Foo f; static_assert(f.DoTheThing() == 1, "DoTheThing() should return 1")

Calling constexpr in default template argument

醉酒当歌 提交于 2019-11-27 16:02:20
In C++11 I am using a constexpr function as a default value for a template parameter - it looks like this: template <int value> struct bar { static constexpr int get() { return value; } }; template <typename A, int value = A::get()> struct foo { }; int main() { typedef foo<bar<0>> type; return 0; } G++ 4.5 and 4.7 compiles this, but Clang++ 3.1 does not. The error message from clang is: clang_test.cpp:10:35: error: non-type template argument is not a constant expression template <typename A, int value = A::get()> ^~~~~~~~ clang_test.cpp:17:19: note: while checking a default template argument

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

非 Y 不嫁゛ 提交于 2019-11-27 16:00:48
Why is this constexpr static member function, identified by the //! Nah comment, not seen as constexpr when called? struct Item_id { enum Enum { size, position, attributes, window_rect, max_window_size, _ }; static constexpr int n_items_ = _; // OK constexpr auto member_n_items() const -> int { return _; } // OK static constexpr auto static_n_items() -> int { return _; } // OK static constexpr int so_far = n_items_; // OK #ifndef OUT_OF_CLASS static constexpr int bah = static_n_items(); //! Nah. #endif }; constexpr auto n_ids() -> int { return Item_id().member_n_items(); } // OK auto main() ->

Is constexpr really needed?

半腔热情 提交于 2019-11-27 14:41:16
I have been looking at the new constexpr feature of C++ and I do not fully understand the need for it. For example, the following code: constexpr int MaxSize() { ... return ...; } void foo() { int vec[MaxSize()]; } can be replaced by: int MaxSize() { ... return ...; } static const int s_maxSize = MaxSize(); foo() { int vec[s_maxSize]; } Update The second example is actually not standard ISO C++ (thanks to several users for pointing this out) but certain compilers (e.g. gcc) support it. So it is not const that makes the program valid, but the fact that gcc supports this non-standard feature.

how to initialize a constexpr reference

淺唱寂寞╮ 提交于 2019-11-27 14:37:52
I am trying to initialize a constexpr reference with no success. I tried #include <iostream> constexpr int& f(int& x) // can define functions returning constexpr references { return x; } int main() { constexpr int x{20}; constexpr const int& z = x; // error here } but I'm getting a compile time error error: constexpr variable 'z' must be initialized by a constant expression Dropping the const results in error: binding of reference to type 'int' to a value of type 'const int' drops qualifiers even though I had the feeling that constexpr automatically implies const for variable declarations. So

Why is C++11 constexpr so restrictive?

 ̄綄美尐妖づ 提交于 2019-11-27 14:17:02
问题 As you probably know, C++11 introduces the constexpr keyword. C++11 introduced the keyword constexpr, which allows the user to guarantee that a function or object constructor is a compile-time constant. [...] This allows the compiler to understand, and verify, that [function name] is a compile-time constant. My question is why are there such strict restrictions on form of the functions that can be declared. I understand desire to guarantee that function is pure, but consider this: The use of

constexpr initialization of array to sort contents

回眸只為那壹抹淺笑 提交于 2019-11-27 14:05:55
问题 This is a bit of a puzzle rather than a real-world problem, but I've gotten into a situation where I want to be able to write something that behaves exactly like template<int N> struct SortMyElements { int data[N]; template<typename... TT> SortMyElements(TT... tt) : data{ tt... } { std::sort(data, data+N); } }; int main() { SortMyElements<5> se(1,4,2,5,3); int se_reference[5] = {1,2,3,4,5}; assert(memcmp(se.data, se_reference, sizeof se.data) == 0); } except that I want the SortMyElements