constexpr

const vs constexpr on variables

北城以北 提交于 2019-11-26 11:02:38
Is there a difference between the following definitions? const double PI = 3.141592653589793; constexpr double PI = 3.141592653589793; If not, which style is preferred in C++11? Howard Hinnant I believe there is a difference. Let's rename them so that we can talk about them more easily: const double PI1 = 3.141592653589793; constexpr double PI2 = 3.141592653589793; Both PI1 and PI2 are constant, meaning you can not modify them. However only PI2 is a compile-time constant. It shall be initialized at compile time. PI1 may be initialized at compile time or run time. Furthermore, only PI2 can be

Is it possible to use std::string in a constexpr?

拈花ヽ惹草 提交于 2019-11-26 10:18:34
问题 Using C++11, Ubuntu 14.04, GCC default toolchain . This code fails: constexpr std::string constString = \"constString\"; error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal... because... ‘std::basic_string’ has a non-trivial destructor Is it possible to use std::string in a constexpr ? (apparently not...) If so, how? Is there an alternative way to use a character string in a constexpr ? 回答1: No, and your compiler already gave you a

Does static constexpr variable inside a function make sense?

余生长醉 提交于 2019-11-26 10:08:42
问题 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? 回答1: The short answer is that not only is static useful, it is pretty well always going to

Difference between “if constexpr()” Vs “if()”

ⅰ亾dé卋堺 提交于 2019-11-26 09:44:56
问题 What is the difference between if constexpr() and if() ? Where and When can I use both of them? 回答1: The ordinary if statement: Has its condition evaluated every time control reaches it, if ever Determines which of the two substatements to execute, skipping the other Requires both substatements to be well-formed regardless of which one is actually selected at runtime The if constexpr statement: Has its condition evaluated at compile time once all necessary template arguments have been

constexpr initializing static member using static function

风格不统一 提交于 2019-11-26 09:03:46
问题 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

Understanding the example on lvalue-to-rvalue conversion

拜拜、爱过 提交于 2019-11-26 08:38:23
问题 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); //

Concat two `const char` string literals

孤者浪人 提交于 2019-11-26 08:25:16
问题 Is it possible to concat two string literals using a constexpr ? Or put differently, can one eliminate macros in code like: #define nl(str) str \"\\n\" int main() { std::cout << nl(\"usage: foo\") nl(\"print a message\") ; return 0; } Update : There is nothing wrong with using \"\\n\" , however I would like to know whether one can use constexpr to replace those type of macros. 回答1: Yes, it is entirely possible to create compile-time constant strings, and manipulate them with constexpr

C++11: Compile Time Calculation of Array

て烟熏妆下的殇ゞ 提交于 2019-11-26 08:08:04
问题 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

Why do we need to mark functions as constexpr?

偶尔善良 提交于 2019-11-26 07:39:40
问题 C++11 allows functions declared with the constexpr specifier to be used in constant expressions such as template arguments. There are stringent requirements about what is allowed to be constexpr ; essentially such a function encapsulates only one subexpression and nothing else. (Edit: this is relaxed in C++14 but the question stands.) Why require the keyword at all? What is gained? It does help in revealing the intent of an interface, but it doesn\'t validate that intent, by guaranteeing that

Lookup table with constexpr

只愿长相守 提交于 2019-11-26 06:37:21
问题 I\'m looking to create a lookup table of coordinates, something like: int a[n][2] = {{0,1},{2,3}, ... } For a given n , to be created at compile time. I started looking into constexpr , but is seems like a function returning a constexpr std::vector<std::array <int, 2> > isn\'t an option, as I get: invalid return type \'std::vector<std::array<int, 2ul> >\' of constexpr function How can create such a compile time array? 回答1: I'll dump the code first, adding references and comments where