constexpr

Is a constexpr array necessarily odr-used when subscripted?

谁说我不能喝 提交于 2019-12-17 06:09:58
问题 Given the following code: struct A { static constexpr int a[3] = {1,2,3}; }; int main () { int a = A::a[0]; int b [A::a[1]]; } is A::a necessarily odr-used in int a = A::a[0] ? Note: This question represents a less flamey/illogical/endless version of a debate in the Lounge. 回答1: First use of A::a : int a = A::a[0]; The initializer is a constant expression, but that doesn't stop A::a from being odr-used here. And, indeed, A::a is odr-used by this expression. Starting from the expression A::a[0

Does constexpr imply inline?

丶灬走出姿态 提交于 2019-12-17 06:04:21
问题 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

Detecting constexpr with SFINAE

余生长醉 提交于 2019-12-17 05:02:10
问题 I'm working on upgrading some C++ code to take advantage of the new functionality in C++11. I have a trait class with a few functions returning fundamental types which would most of the time, but not always, return a constant expression. I would like to do different things based on whether the function is constexpr or not. I came up with the following approach: template<typename Trait> struct test { template<int Value = Trait::f()> static std::true_type do_call(int){ return std::true_type();

constexpr and initialization of a static const void pointer with reinterpret cast, which compiler is right?

社会主义新天地 提交于 2019-12-17 03:43:38
问题 Consider the following piece of code: struct foo { static constexpr const void* ptr = reinterpret_cast<const void*>(0x1); }; auto main() -> int { return 0; } The above example compiles fine in g++ v4.9 (Live Demo), while it fails to compile in clang v3.4 (Live Demo) and generates the following error: error: constexpr variable 'ptr' must be initialized by a constant expression Questions: Which of the two compilers is right according to the standard? What's the proper way of declaring an

constexpr and initialization of a static const void pointer with reinterpret cast, which compiler is right?

徘徊边缘 提交于 2019-12-17 03:43:12
问题 Consider the following piece of code: struct foo { static constexpr const void* ptr = reinterpret_cast<const void*>(0x1); }; auto main() -> int { return 0; } The above example compiles fine in g++ v4.9 (Live Demo), while it fails to compile in clang v3.4 (Live Demo) and generates the following error: error: constexpr variable 'ptr' must be initialized by a constant expression Questions: Which of the two compilers is right according to the standard? What's the proper way of declaring an

const vs constexpr on variables

时光怂恿深爱的人放手 提交于 2019-12-16 23:05:21
问题 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? 回答1: 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

Is the address of a local variable a constexpr?

本秂侑毒 提交于 2019-12-14 03:40:23
问题 In Bjarne Stroustrup's book "The C++ Programming Language (4th Edition)" on p. 267 (Section 10.4.5 Address Constant Expressions), he uses a code example where the address of a local variable is set to a constexpr variable. I thought this looked odd, so I tried running the example with g++ version 7.3.0 and was unable to get the same results. Here is his code example verbatim (although slightly abridged): extern char glob; void f(char loc) { constexpr const char* p0 = &glob; // OK: &glob's is

Which values can be assigned to a `constexpr` reference?

拈花ヽ惹草 提交于 2019-12-13 19:21:37
问题 Original Question I would like to use static member variables in order to pass information via a type template parameter into templated classes. These variables should not be set in a header file that is included in all translation units so that I can change them without recompiling most of the object files. Moreover, it would be nice to have a handy alias for the variable that does not require additional space. I thought that constexpr read-only references like static constexpr const int&

constexpr function and its parameter

泪湿孤枕 提交于 2019-12-13 18:21:28
问题 constexpr int hello(int j) { return j * 12; } constexpr int bye(int j = 6) { return j * 12; } int main() { int i = 6; constexpr int a1 = hello(i); //error constexpr int a2 = hello(6); //ok constexpr int a3 = hello(int(6)); //ok constexpr int a4 = bye(); //ok constexpr int a5 = bye(i); //error constexpr int a6 = bye(6); //ok return 0; } What's difference between hellow(i) and hello(6) ? I think one is int j = i; and j is not a constexpr, while the other is int j = 6 and j is still not a

`static constexpr` function called in a constant expression is…an error?

狂风中的少年 提交于 2019-12-13 15:42:54
问题 I have the following code: class MyClass { static constexpr bool foo() { return true; } void bar() noexcept(foo()) { } }; I would expect that since foo() is a static constexpr function, and since it's defined before bar is declared, this would be perfectly acceptable. However, g++ gives me the following error: error: ‘static constexpr bool MyClass::foo()’ called in a constant expression This is...less than helpful, since the ability to call a function in a constant expression is the entire