constexpr

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

左心房为你撑大大i 提交于 2019-11-29 10:44:34
问题 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

Constexpr compile error using std::acos with clang++ not g++

ⅰ亾dé卋堺 提交于 2019-11-29 10:29:50
I want to experiment with migrating a project from gcc to clang++. I admit ignorance on my part, I'm not sure why the following bit of code template <typename T> constexpr T pi{std::acos(T(-1.0))}; compiles silently with g++ but clang++ produces the error trig.hpp:3:13: error: constexpr variable 'pi<float>' must be initialized by a constant expression constexpr T pi{std::acos(T(-1.0))}; and I was hoping someone who knows more about it than I do could enlighten me. NB: Tried with -std=C++14 and C++1y. Fails under clang version 3.6.2 (tags/RELEASE_362/final). Works with g++ (GCC) 5.2.0. Shafik

Initializing static constexpr variables and classes inside a struct

不问归期 提交于 2019-11-29 09:34:48
Here is my working code example: #include <iostream> template<typename B> class b { public: int y; constexpr b(int x) : y(x) { } constexpr void sayhi() { std::cout << "hi" << std::endl; } }; template<int x> struct A { static constexpr b<int> bee = x; static constexpr int y = x; // this one is fine and usable already, I don't have to do something like what I did on member bee inline static void sayhi() { std::cout << y << std::endl; } }; template<int x> constexpr b<int> A<x>::bee; // why do I have to do something like this, it will cause errors if I don't. int main(int argc, char** argv) { A<30

Undefined reference error for static constexpr member

耗尽温柔 提交于 2019-11-29 09:17:11
Consider this code: #include <vector> struct A { static constexpr int kDefaultValue = -1; std::vector<int> v; A(int n): v(n, A::kDefaultValue) {} }; int main() { A(10); return 0; } It fails to link (llvm clang, gcc 4.9, both on OS X): Undefined symbols for architecture x86_64: "A::kDefaultValue", referenced from: A::(int) in main.cpp.o ld: symbol(s) not found for architecture x86_64 The question is what's wrong with it? It can be fixed by static_cast -ing A::kDefaultValue to int . Or by moving kDefaultValue out of A . Both cases seem to be ugly. Is this another way to make it link? This

std::max() and std::min() not constexpr

心已入冬 提交于 2019-11-29 09:03:35
I just noticed that the new standard defines min(a,b) and max(a,b) without constexpr . Examples from 25.4.7, [alg.min.max]: template<class T> const T& min(const T& a, const T& b); template<class T> T min(initializer_list<T> t); Isn't this a pity? I would have liked to write char data[ max(sizeof(A),sizeof(B)) ]; instead of char data[ sizeof(A) > sizeof(B) ? sizeof(A) : sizeof(B) ]; char data[ MAX(sizeof(A),sizeof(B)) ]; // using a macro Any reason why those can not be constexpr ? Critical Update The below analysis is wrong, because it confuses one important thing . The following statement I

c++11 fast constexpr integer powers

▼魔方 西西 提交于 2019-11-29 06:29:51
Beating the dead horse here. A typical (and fast) way of doing integer powers in C is this classic: int64_t ipow(int64_t base, int exp){ int64_t result = 1; while(exp){ if(exp & 1) result *= base; exp >>= 1; base *= base; } return result; } However I needed a compile time integer power so I went ahead and made a recursive implementation using constexpr: constexpr int64_t ipow_(int base, int exp){ return exp > 1 ? ipow_(base, (exp>>1) + (exp&1)) * ipow_(base, exp>>1) : base; } constexpr int64_t ipow(int base, int exp){ return exp < 1 ? 1 : ipow_(base, exp); } The second function is only to

Nested struct breaks constexpr despite being identical to global ones

流过昼夜 提交于 2019-11-29 06:11:46
问题 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

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

China☆狼群 提交于 2019-11-29 06:11:20
问题 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

x[0] == 1 constant expression in C++11 when x is const int[]?

十年热恋 提交于 2019-11-29 06:04:46
问题 Is the following C++11 program ill-formed? const int x[] = {1,2,3}; static_assert(x[0] == 1, "yay"); int main() {} gcc and clang seem to think so, but why isn't x[0] == 1 a constant expression? x[0] == 1 subscript operator *(x+0) == 1 array-to-pointer conversion (int* p = x) *(p+0) == 1 pointer addition *p == 1 indirection (lvalue y = x[0]) y == 1 lvalue-to-rvalue conversion: a non-volatile glvalue (yes, x[0] is a glvalue and non-volatile) of integral (yes it has type const int) or

C++11 constexpr function's argument passed in template argument

守給你的承諾、 提交于 2019-11-29 05:47:09
This used to work some weeks ago: template <typename T, T t> T tfunc() { return t + 10; } template <typename T> constexpr T func(T t) { return tfunc<T, t>(); } int main() { std::cout << func(10) << std::endl; return 0; } But now g++ -std=c++0x says: main.cpp: In function ‘constexpr T func(T) [with T = int]’: main.cpp:29:25: instantiated from here main.cpp:24:24: error: no matching function for call to ‘tfunc()’ main.cpp:24:24: note: candidate is: main.cpp:16:14: note: template<class T, T t> T tfunc() main.cpp:25:1: warning: control reaches end of non-void function [-Wreturn-type] clang++ -std