constexpr

Why is calling a constexpr function with a member array not a constant expression?

烂漫一生 提交于 2019-12-03 09:28:47
I have the following helper function: template<typename T, std::size_t N> constexpr std::size_t Length(const T(&)[N]) { return N; } Which returns the length of a static array. In the past this always has worked but when I do this: struct Foo { unsigned int temp1[3]; void Bar() { constexpr std::size_t t = Length(temp1); // Error here } }; I get an error when using MSVS 2017: error C2131: expression did not evaluate to a constant note: failure was caused by a read of a variable outside its lifetime note: see usage of 'this' I was hoping someone can shed light on what I'm doing wrong. Rakete1111

use of constexpr in header file

吃可爱长大的小学妹 提交于 2019-12-03 09:28:47
问题 I can have a definition like this in a header file? constexpr double PI=3.14; Is there any problem in having this in a header file that would be included to several cpp files? I am worried that since it says in standard that this constexpr has its own memory, putting it in header, and adding header to several cpp files, generate multiple copies of the same value in memory and some other nasty problems. I am using C++11 回答1: constexpr implies const and const on global/namespace scope implies

Access to constexpr variable inside lambda expression without capturing

落爺英雄遲暮 提交于 2019-12-03 09:24:12
In the following example, I can access the constexpr variable x from inside the lambda y without explicitly capturing it. This is not possible if x is not declared as constexpr . Are there special rules that apply to constexpr for capturing? int foo(auto l) { // OK constexpr auto x = l(); auto y = []{return x;}; return y(); // NOK // auto x2 = l(); // auto y2 = []{ return x2; }; // return y2(); } auto l2 = []{return 3;}; int main() { foo(l2); } Are there special rules that apply to constexpr for capturing/accessing? Yes, constexpr variables could be read without capturing in lambda : A lambda

Determine `constexpr` execution - during compilation or at runtime?

给你一囗甜甜゛ 提交于 2019-12-03 09:02:58
问题 Is there a way to achieve different behaviour of a constexpr function in the compilation phase and at runtime? Consider the following example (using a theoretical feature from D: static if ): constexpr int pow( int base , int exp ) noexcept { static if( std::evaluated_during_translation() ) { auto result = 1; for( int i = 0 ; i < exp ; i++ ) result *= base; return result; } else { // std::evaluated_during_runtime() return std::pow( base , exp ); } } If not, is there a way to restrict

Why do we need the two definitions: integral constant expression and converted constant expression?

非 Y 不嫁゛ 提交于 2019-12-03 08:46:31
问题 §5.19/3 in C++14 defines an integral constant expression and a converted constant expression: An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. [ Note: Such expressions may be used as array bounds (8.3.4, 5.3.4), as bit-field lengths (9.6), as enumerator initializers if the underlying type is not fixed (7.2), and as alignments (7.6.2). —end note ] A

What is constexpr in C++?

走远了吗. 提交于 2019-12-03 06:00:45
I am really confused about a constexpr concept, as I have read constexpr is evaluated at compile time, so it is useful for performance optimization versus normal const . constexpr int i = 0; constexpr int& ri = i; The above code returns an error "invalid initialization of reference of type 'int&' from expression of type 'const int'", why? Also, the next code has an error: constexpr int i = 0; constexpr int* ri = &i; If I replaced the constexpr keyword with const , all above worked correctly. Cheers and hth. - Alf Re ” if I replaced the constexpr word with const all above worked correctly." A

why declare constrexpr constructors for classes with non-trivial destructors (e.g. unique_ptr, std::variant)

旧时模样 提交于 2019-12-03 05:07:45
As far as I understand (at least for c++14 ), a destructor cannot be constexpr if it is not trivial (implicit generated or =default ). What is the point of declaring constexpr constructors for structures with non-trivial destructors? struct X { int a_; constexpr X(int a) : a_{a} {} // constexpr ~X(){}; // Error dtor cannot be marked constexpr // ~X(){}; // causes error at y declaration: temporary of non-literal type ‘X’ // in a constant expression . }; template <int N> struct Y {}; int main() { Y<X{3}.a_> y; // OK only if the destructor is trivial (void)y; } // tested with c++14 g++-5.1.0 and

Global constants in C++11

夙愿已清 提交于 2019-12-03 05:03:00
What are the best ways to declare and define global constants in C++? I am mostly interested in C++11 standard as it fixes a lot in this regard. [EDIT (clarification)]: in this question "global constant" denotes constant variable or function that is known at compile time in any scope. Global constant must be accessible from more than one translation unit. It is not necessarily constexpr-style constant - can be something like const std::map<int, std::string> m = { { 1, "U" }, { 5, "V" } }; or const std::map<int, std::string> * mAddr() { return & m; } . I do not touch preferable good-style scope

Modern C++: initialize constexpr tables

元气小坏坏 提交于 2019-12-03 04:47:38
Suppose I have a class X , which functionality requires a lot of constant table values, say an array A[1024] . I have a recurrent function f that computes its values, smth like A[x] = f(A[x - 1]); Suppose that A[0] is a known constant, therefore the rest of the array is constant too. What is the best way to calculate these values beforehand, using features of modern C++, and without storaging file with hardcoded values of this array? My workaround was a const static dummy variable: const bool X::dummy = X::SetupTables(); bool X::SetupTables() { A[0] = 1; for (size_t i = 1; i <= A.size(); ++i)

Can adding 'constexpr' change the behaviour?

好久不见. 提交于 2019-12-03 04:26:06
Given two programs where the only difference in the source code is the presence or absence of one constexpr , is it possible that the meaning of the program changes? In other words, if there was a compiler option to ask the compiler to try really hard to infer constexpr where possible, would it break existing standard code and/or change its meaning in bad ways? Imagine dealing with a codebase where the original developer forgot to include constexpr in places where it was possible, perhaps code written before C++11. It would be great if the compiler would infer constexpr to help you get on with