constexpr

Does static constexpr variable inside a function make sense?

岁酱吖の 提交于 2019-11-27 02:30:25
If I have a variable inside a function (say, a large array), does it make sense to declare it both static and constexpr ? constexpr guarantees that the array is created at compile time, so would the static be useless? void f() { static constexpr int x [] = { // a few thousand elements }; // do something with the array } Is the static actually doing anything there in terms of generated code or semantics? rici The short answer is that not only is static useful, it is pretty well always going to be desired. First, note that static and constexpr are completely independent of each other. static

constexpr initializing static member using static function

你说的曾经没有我的故事 提交于 2019-11-27 02:07:33
Requirements I want a constexpr value (i.e. a compile-time constant) computed from a constexpr function. And I want both of these scoped to the namespace of a class, i.e. a static method and a static member of the class. First attempt I first wrote this the (to me) obvious way: class C1 { constexpr static int foo(int x) { return x + 1; } constexpr static int bar = foo(sizeof(int)); }; g++-4.5.3 -std=gnu++0x says to that: error: ‘static int C1::foo(int)’ cannot appear in a constant-expression error: a function call cannot appear in a constant-expression g++-4.6.3 -std=gnu++0x complains: error:

Which parts of the C++14 Standard Library could be and which parts will be made constexpr?

一个人想着一个人 提交于 2019-11-27 02:03:23
问题 With the new relaxed C++14 constexpr rules, compile-time programming becomes a lot more expressive. I wonder whether the Standard Library will also be upgraded to take advantage. In particular, std::initializer_list , std::pair , std::tuple , std::complex , std::bitset and std::array seem like prime candidates to be marked constexpr wholesale. Questions : which parts of the Standard Library will now be marked constexpr ? which other parts could be marked constexpr ? e.g. why aren't the

Compile time string encryption using constexpr

若如初见. 提交于 2019-11-27 01:26:42
问题 I want to have a compile-time string encryption, such that I could write in my code: const auto encryptedInvalidLicense = ENCRYPT("Invalid license"); std::cout << encryptedInvalidLicense.decrypt() << std::endl; // outputs "Invalid license" and the string "Invalid license" wont appear in the binaries. Pre-builds might be the answer, but I'm looking for a pure c++ constexpr solution to this problem, and that it will be supported by VS2015. Any suggestions? I've already looked into Compile-time

Does constexpr imply inline?

我的梦境 提交于 2019-11-27 01:14:57
Consider the following inlined function : // Inline specifier version #include<iostream> #include<cstdlib> inline int f(const int x); inline int f(const int x) { return 2*x; } int main(int argc, char* argv[]) { return f(std::atoi(argv[1])); } and the constexpr equivalent version : // Constexpr specifier version #include<iostream> #include<cstdlib> constexpr int f(const int x); constexpr int f(const int x) { return 2*x; } int main(int argc, char* argv[]) { return f(std::atoi(argv[1])); } My question is : does the constexpr specifier imply the inline specifier in the sense that if a non-constant

Compile-time or runtime detection within a constexpr function

余生长醉 提交于 2019-11-27 00:56:08
问题 I was excited when constexpr was introduced in C++11, but I unfortunately made optimistic assumptions about its usefulness. I assumed that we could use constexpr anywhere to catch literal compile-time constants or any constexpr result of a literal compile-time constant, including something like this: constexpr float MyMin(constexpr float a, constexpr float b) { return a<b?a:b; } Because qualifying a function's return type only as constexpr does not limit its usage to compile-time, and must

Why isn't `std::initializer_list` defined as a literal type?

那年仲夏 提交于 2019-11-27 00:40:43
问题 This is a follow-up of this question: Is it legal to declare a constexpr initializer_list object?. Since C++14, the std::initializer_list class has all of its methods marked with constexpr . It seems natural to be able to initialize an instance by doing constexpr std::initializer_list<int> list = {1, 2, 3}; but Clang 3.5 complains about list not being initialized by a constant expression. As dyp pointed out in a comment, any requirement for std::initializer_list to be a literal type seem to

C++11: Compile Time Calculation of Array

荒凉一梦 提交于 2019-11-27 00:26:28
Suppose I have some constexpr function f: constexpr int f(int x) { ... } And I have some const int N known at compile time: Either #define N ...; or const int N = ...; as needed by your answer. I want to have an int array X: int X[N] = { f(0), f(1), f(2), ..., f(N-1) } such that the function is evaluated at compile time, and the entries in X are calculated by the compiler and the results are placed in the static area of my application image exactly as if I had used integer literals in my X initializer list. Is there some way I can write this? (For example with templates or macros and so on)

Why is comparing two parameters of a constexpr function not a constant condition for static assertion?

我的梦境 提交于 2019-11-26 23:22:54
问题 constexpr uint32_t BitPositionToMask(int i,int Size){ static_assert(i < Size,"bit position out of range"); return 1 << i; } this generates: error: non-constant condition for static assertion on GCC 4.6.2 Am I not getting something or is this a GCC bug? Update: thank you Andy for being my nerd guardian angel again. Since I have the values at compile time anway I just made it a template and it works as intended. template<int i,int Size> constexpr uint32_t BitPositionToMask(){ static_assert(i <

Understanding the example on lvalue-to-rvalue conversion

吃可爱长大的小学妹 提交于 2019-11-26 23:17:22
I have a hard time understanding how this code (an example from the C++14 draft standard [conv.lval] ) invokes undefined behavior for g(false) . Why does constexpr make the program valid? Also, what does it mean by "does not access y.n "? In both calls to g() we are returning the n data member so why does the last line say it doesn't access it? struct S { int n; }; auto f() { S x { 1 }; constexpr S y { 2 }; return [&](bool b) { return (b ? y : x).n; }; } auto g = f(); int m = g(false); // undefined behavior due to access of x.n outside its // lifetime int n = g(true); // OK, does not access y