constexpr

Static constexpr odr-used or not?

纵然是瞬间 提交于 2019-11-26 12:47:41
问题 How come that the following works on gcc but doesn\'t on clang , ( see it live ): constexpr int giveMeValue() { return 42; } struct TryMe { static constexpr int arr[1] = { giveMeValue() }; }; int main() { int val = TryMe::arr[0]; return val; } I get an unresolved external symbol with clang. Is TryMe::arr[0] an object? If it is, is it odr-used? 回答1: TryMe::arr is odr-used but you don't provide a definition ( see it live ): constexpr int TryMe::arr[1]; Why is the result inconsistent between gcc

Self-initialization of a static constexpr variable, is it well-formed?

对着背影说爱祢 提交于 2019-11-26 12:29:24
问题 Given the following declaration in the global namespace: constexpr int x = x; Is this well-formed? The draft C++14 standard section 3.6.2 [basic.start.init] says: Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place. [...] What seems to make the example well defined is that x is initialized with its own value during constant initialization which will be 0 due to zero initialization. Is

Constexpr Math Functions

前提是你 提交于 2019-11-26 12:26:46
问题 So noticed from this page that none of the math functions in c++11 seems to make use of constexpr, whereas I believe all of them could be. So that leaves me with two questions, one is why did they choose not to make the functions constexpr. And two for a function like sqrt I could probably write my own constexpr, but something like sin or cos would be trickier so is there a way around it. 回答1: Actually, because of old and annoying legacy, almost none of the math functions can be constexpr ,

Populate An Array Using Constexpr at Compile-time

 ̄綄美尐妖づ 提交于 2019-11-26 12:25:59
问题 I would like to populate an array of enum using constexpr. The content of the array follows a certain pattern. I have an enum separating ASCII character set into four categories. enum Type { Alphabet, Number, Symbol, Other, }; constexpr Type table[128] = /* blah blah */; I would like to have an array of 128 Type . They can be in a structure. The index of the array will be corresponding to the ASCII characters and the value will be the Type of each character. So I can query this array to find

Create N-element constexpr array in C++11

a 夏天 提交于 2019-11-26 12:16:19
Hello i'm learning C++11, I'm wondering how to make a constexpr 0 to n array, for example: n = 5; int array[] = {0 ... n}; so array may be {0, 1, 2, 3, 4, 5} In C++14 it can be easily done with a constexpr constructor and a loop: #include <iostream> template<int N> struct A { constexpr A() : arr() { for (auto i = 0; i != N; ++i) arr[i] = i; } int arr[N]; }; int main() { constexpr auto a = A<4>(); for (auto x : a.arr) std::cout << x << '\n'; } Kal Unlike those answers in the comments to your question, you can do this without compiler extensions. #include <iostream> template<int N, int... Rest>

Is is_constexpr possible in C++11?

偶尔善良 提交于 2019-11-26 11:49:43
Is it possible to produce a compile-time boolean value based on whether or not a C++11 expression is a constant expression (i.e. constexpr ) in C++11? A few questions on SO relate to this, but I don't see a straight answer anywhere. As of 2017, is_constexpr is not possible in C++11. That sounds like an odd thing to say, so let me explain a bit of the history. First, we added this feature to resolve a defect: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1129 Johannes Schaub - litb posted a constexpr detection macro that relied on the provision that constant expressions are

constexpr if and static_assert

不问归期 提交于 2019-11-26 11:29:00
问题 P0292R1 constexpr if has been included, on track for C++17. It seems useful (and can replace use of SFINAE), but a comment regarding static_assert being ill-formed, no diagnostic required in the false branch scares me: Disarming static_assert declarations in the non-taken branch of a constexpr if is not proposed. void f() { if constexpr (false) static_assert(false); // ill-formed } template<class T> void g() { if constexpr (false) static_assert(false); // ill-formed; no // diagnostic required

How to declare constexpr extern?

ぃ、小莉子 提交于 2019-11-26 11:24:38
问题 Is it possible to declare a variable extern constexpr and define it in another file? I tried it but the compiler gives error: Declaration of constexpr variable \' i \' is not a definition in .h: extern constexpr int i; in .cpp: constexpr int i = 10; 回答1: no you can't do it, here's what the standard says (section 7.1.5): 1 The constexpr specifier shall be applied only to the definition of a variable or variable template, the declaration of a function or function template, or the declaration of

Why isn&#39;t a for-loop a compile-time expression?

China☆狼群 提交于 2019-11-26 11:22:54
问题 If I want to do something like iterate over a tuple, I have to resort to crazy template metaprogramming and template helper specializations. For example, the following program won\'t work: #include <iostream> #include <tuple> #include <utility> constexpr auto multiple_return_values() { return std::make_tuple(3, 3.14, \"pi\"); } template <typename T> constexpr void foo(T t) { for (auto i = 0u; i < std::tuple_size<T>::value; ++i) { std::get<i>(t); } } int main() { constexpr auto ret = multiple

Is it legal to declare a constexpr initializer_list object?

帅比萌擦擦* 提交于 2019-11-26 11:17:01
问题 As a question that came up during the discussion of this SO question: Is it legal, maybe with N3471, to declare a constexpr std::initializer_list object? Example: constexpr std::initializer_list<int> my_list{}; Why I think it may not be legal: initializer_list would have to be a literal type; but are there any guarantees that it is a literal type? Citations from N3485. [dcl.constexpr]/9: A constexpr specifier used in an object declaration declares the object as const. Such an object shall