constexpr

Why is non-const std::array::operator[] not constexpr?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 07:57:28
I'm trying to fill a 2D array on compile time with a given function. Here is my code: template<int H, int W> struct Table { int data[H][W]; //std::array<std::array<int, H>, W> data; // This does not work constexpr Table() : data{} { for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) data[i][j] = i * 10 + j; // This does not work with std::array } }; constexpr Table<3, 5> table; // I have table.data properly populated at compile time It works just fine, table.data is properly populated at compile time. However, if I change plain 2D array int[H][W] with std::array<std::array<int, H>, W> , I

Nested struct breaks constexpr despite being identical to global ones

一笑奈何 提交于 2019-11-30 06:47:11
I'm having trouble with the following code: template<typename T> constexpr int get(T vec) { return vec.get(); } struct coord { constexpr int get() const { return x; } int x; }; struct foo { struct coord2 { constexpr int get() const { return x; } int x; }; constexpr static coord f = { 5 }; constexpr static int g = get(f); // works constexpr static coord2 h = { 5 }; constexpr static int i = get(h); // doesn't work }; constexpr coord foo::f; constexpr foo::coord2 foo::h; int main(){} Essentially, get(f) is considered a constant expression, but get(h) is not. The only thing changed is that one

Where in the C++11 standard does it specify when a constexpr function can be evaluated during translation?

白昼怎懂夜的黑 提交于 2019-11-30 06:28:40
Just because a function (or constructor)... is declared constexpr and the function definition meets the constexpr requirements ...doesn't mean that the compiler will evaluate the constexpr function during translation. I've been looking through the C++11 FDIS (N3242, available at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/ ) to try and determine two things: When is the compiler obligated to evaluate a constexpr function during translation? When is the compiler allowed to evaluate a constexpr function during translation? Section 5.19 Paragraph 1 says that constant expressions can be

Could non-static member variable be modified in constexpr constructor (C++14)?

冷暖自知 提交于 2019-11-30 03:13:33
问题 struct A { int a = 0; constexpr A() { a = 1; } }; constexpr bool f() { constexpr A a; static_assert(a.a == 1, ""); // L1: OK return a.a == 1; } static_assert(f(), ""); // L2: Error, can not modify A::a in constexpr Online Compiler URL: http://goo.gl/jni6Em Compiler: clang 3.4 (with -std=c++1y) System: Linux 3.2 If I delete L2, this code compiles. If I add L2, the compiler complained "modification of object of const-qualified type 'const int' is not allowed in a constant expression". I am not

How undefined are __builtin_ctz(0) or __builtin_clz(0)?

岁酱吖の 提交于 2019-11-30 03:11:50
Background For a long time, gcc has been providing a number of builtin bit-twiddling functions, in particular the number of trailing and leading 0-bits (also for long unsigned and long long unsigned , which have suffixes l and ll ): — Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position. If x is 0, the result is undefined. — Built-in Function: int __builtin_ctz (unsigned int x) Returns the number of trailing 0-bits in x , starting at the least significant bit position. If x is 0, the result is undefined.

constexpr std::array with static_assert

我只是一个虾纸丫 提交于 2019-11-30 02:40:27
问题 #include <iostream> #include <array> int main(int argc, char **argv) { constexpr const std::array<int, 2> arr {{ 0, 1 }}; constexpr const int arr2[] = { 0, 1}; static_assert(arr[0] == arr2[0], "asdf"); static_assert(arr[1] == arr2[1], "asdfasdf"); return 0; } When compiled with gcc 4.8.2 and 4.9.1 using g++ test.cpp --std=c++11 , the compilation succeeds. When compiled with clang 3.4 and 3.5 using clang++ test.cpp --std=c++11 however, the compilation fails: test.cpp:8:16: error: static_assert

How can the compile-time be (exponentially) faster than run-time?

£可爱£侵袭症+ 提交于 2019-11-30 02:39:42
The below code calculates Fibonacci numbers by an exponentially slow algorithm: #include <cstdlib> #include <iostream> #define DEBUG(var) { std::cout << #var << ": " << (var) << std::endl; } constexpr auto fib(const size_t n) -> long long { return n < 2 ? 1: fib(n - 1) + fib(n - 2); } int main(int argc, char *argv[]) { const long long fib91 = fib(91); DEBUG( fib91 ); DEBUG( fib(45) ); return EXIT_SUCCESS; } And I am calculating the 45th Fibonacci number at run-time, and the 91st one at compile time. The interesting fact is that GCC 4.9 compiles the code and computes fib91 in a fraction of a

Unique address for constexpr variable

痞子三分冷 提交于 2019-11-30 02:30:33
问题 Is it possible to have a unique address allocated for a constexpr variable, i.e. the same for all translation units where the variable is available (usually through a header)? Consider the following example: // foo.hh #include <iostream> constexpr int foo = 42; // a.cc #include "foo.hh" void a(void) { std::cout << "a: " << &foo << std::endl; } // b.cc #include "foo.hh" extern void a(void); int main(int argc, char** argv) { a(); std::cout << "b: " << &foo << std::endl; } Compiling a.cc and b

Undefined symbols for constexpr function

拜拜、爱过 提交于 2019-11-30 01:47:12
问题 When I attempt compiling the following code I get a linker error: Undefined symbols for architecture x86_64: "Foo()", referenced from: _main in main.o using LLVM 4.2. This behavior only occurs when the function is marked constexpr . The program compiles and links correctly when the function is marked const . Why does declaring the function constexpr cause a linker error? (I realize that writing the function this way doesn't give the benefit of compile-time computation; at this point I am

Why can't non-static data members be constexpr?

假装没事ソ 提交于 2019-11-30 00:43:32
This is valid code: struct S { constexpr S(int x, int y): xVal(x), yVal(y) {} constexpr S(int x): xVal(x) {} constexpr S() {} const int xVal { 0 }; const int yVal { 0 }; }; But here I'd really like to declare xVal and yVal constexpr --like this: struct S { constexpr S(int x, int y): xVal(x), yVal(y) {} constexpr S(int x): xVal(x) {} constexpr S() {} constexpr int xVal { 0 }; // error! constexpr int yVal { 0 }; // error! }; As indicated, the code won't compile. The reason is that (per 7.1.5/1), only static data members may be declared constexpr . But why ? Think about what constexpr means. It