constexpr

Computing length of a C string at compile time. Is this really a constexpr?

偶尔善良 提交于 2019-11-26 05:25:26
问题 I\'m trying to compute the length of a string literal at compile time. To do so I\'m using following code: #include <cstdio> int constexpr length(const char* str) { return *str ? 1 + length(str + 1) : 0; } int main() { printf(\"%d %d\", length(\"abcd\"), length(\"abcdefgh\")); } Everything works as expected, the program prints 4 and 8. The assembly code generated by clang shows that the results are computed at compile time: 0x100000f5e: leaq 0x35(%rip), %rdi ; \"%d %d\" 0x100000f65: movl $0x4

Is constexpr supported with lambda functions / expressions?

笑着哭i 提交于 2019-11-26 04:53:40
问题 struct Test { static const int value = []() -> int { return 0; } (); }; With gcc-4.6 I get something like, error: function needs to be constexpr . I have tried multiple combinations of putting constexpr at various places, but no luck. Is constexpr supported for lambda functions as well (irrespective of return type specified or not) ? What is the correct syntax ? Any work around possible ? 回答1: Update : As of C++17, lambdas are permitted in constant expressions. Lambdas are currently (C++14)

C++11 - static_assert within constexpr function?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 04:46:42
问题 How would one properly do a static_assert within a constexpr function? For example: constexpr int do_something(int x) { static_assert(x > 0, \"x must be > 0\"); return x + 5; } This is not valid C++11 code, because a constexpr function must only contain a return statement. I don\'t think that the standard has an exception to this, although, the GCC 4.7 does not let me compile this code. 回答1: This is not valid C++11 code, because a constexpr function must only contain a return statement. This

Create N-element constexpr array in C++11

前提是你 提交于 2019-11-26 03:34:00
问题 Hello i\'m learning C++11, I\'m wondering how to make a constexpr 0 to n array, for example: n = 5; int array[] = {0 ... n}; so array may be {0, 1, 2, 3, 4, 5} 回答1: In C++14 it can be easily done with a constexpr constructor and a loop: #include <iostream> template<int N> struct A { constexpr A() : arr() { for (auto i = 0; i != N; ++i) arr[i] = i; } int arr[N]; }; int main() { constexpr auto a = A<4>(); for (auto x : a.arr) std::cout << x << '\n'; } 回答2: Unlike those answers in the comments

Is is_constexpr possible in C++11?

梦想与她 提交于 2019-11-26 02:37:20
问题 Is it possible to produce a compile-time boolean value based on whether or not a C++11 expression is a constant expression (i.e. constexpr ) in C++11? A few questions on SO relate to this, but I don\'t see a straight answer anywhere. 回答1: As of 2017, is_constexpr is not possible in C++11. That sounds like an odd thing to say, so let me explain a bit of the history. First, we added this feature to resolve a defect: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1129 Johannes

When does a constexpr function get evaluated at compile time?

心已入冬 提交于 2019-11-26 01:29:00
Since it is possible that a function declared as constexpr can be called during run-time, under which criteria does the compiler decide whether to compute it at compile-time or during runtime? template<typename base_t, typename expo_t> constexpr base_t POW(base_t base, expo_t expo) { return (expo != 0 )? base * POW(base, expo -1) : 1; } int main(int argc, char** argv) { int i = 0; std::cin >> i; std::cout << POW(i, 2) << std::endl; return 0; } In this case, i is unknown at compile-time, which is probably the reason why the compiler treats POW() as a regular function which is called at runtime.

When should you use constexpr capability in C++11?

纵然是瞬间 提交于 2019-11-26 01:04:39
问题 It seems to me that having a \"function that always returns 5\" is breaking or diluting the meaning of \"calling a function\". There must be a reason, or a need for this capability or it wouldn\'t be in C++11. Why is it there? // preprocessor. #define MEANING_OF_LIFE 42 // constants: const int MeaningOfLife = 42; // constexpr-function: constexpr int MeaningOfLife () { return 42; } It seems to me that if I wrote a function that return a literal value, and I came up to a code-review, someone

When does a constexpr function get evaluated at compile time?

橙三吉。 提交于 2019-11-26 00:54:59
问题 Since it is possible that a function declared as constexpr can be called during run-time, under which criteria does the compiler decide whether to compute it at compile-time or during runtime? template<typename base_t, typename expo_t> constexpr base_t POW(base_t base, expo_t expo) { return (expo != 0 )? base * POW(base, expo -1) : 1; } int main(int argc, char** argv) { int i = 0; std::cin >> i; std::cout << POW(i, 2) << std::endl; return 0; } In this case, i is unknown at compile-time, which

Why do constant expressions have an exclusion for undefined behavior?

隐身守侯 提交于 2019-11-26 00:48:27
问题 I was researching what is allowed in a core constant expression*, which is covered in section 5.19 Constant expressions paragraph 2 of the draft C++ standard which says: A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression (3.2), but subexpressions of logical AND (5.14), logical OR (5.15), and conditional (5.16) operations that are not evaluated are not considered [ Note: An overloaded operator invokes a

Undefined reference to static constexpr char[]

烂漫一生 提交于 2019-11-26 00:20:57
问题 I want to have a static const char array in my class. GCC complained and told me I should use constexpr , although now it\'s telling me it\'s an undefined reference. If I make the array a non-member then it compiles. What is going on? // .hpp struct foo { void bar(); static constexpr char baz[] = \"quz\"; }; // .cpp void foo::bar() { std::string str(baz); // undefined reference to baz } 回答1: Add to your cpp file: constexpr char foo::baz[]; Reason: You have to provide the definition of the