constexpr

How to declare constexpr extern?

懵懂的女人 提交于 2019-11-27 05:14:10
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; 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 a static data member of a literal type (3.9). If any declaration of a function, function template, or

Difference between “if constexpr()” Vs “if()”

我是研究僧i 提交于 2019-11-27 04:38:27
What is the difference between if constexpr() and if() ? Where and When can I use both of them? The ordinary if statement: Has its condition evaluated every time control reaches it, if ever Determines which of the two substatements to execute, skipping the other Requires both substatements to be well-formed regardless of which one is actually selected at runtime The if constexpr statement: Has its condition evaluated at compile time once all necessary template arguments have been supplied Determines which of the two substatements to compile, discarding the other Does not require the discarded

Is it legal to declare a constexpr initializer_list object?

我的未来我决定 提交于 2019-11-27 04:37:38
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 have literal type and shall be initialized. literal types requirements, [basic.types]/10, sub-bullet

Forcing a constant expression to be evaluated during compile-time?

跟風遠走 提交于 2019-11-27 04:34:19
问题 A few days ago I asked by which criteria the compiler decides whether or not, to compute a constexpr function during compile time. When does a constexpr function get evaluated at compile time? As it turns out, a constexpr is only evaluated during compile-time, if all parameters are constant expressions and the variable you are assigning it to is are constant expression as well. template<typename base_t, typename expo_t> constexpr base_t POW(base_t base, expo_t expo) { return (expo != 0 )?

constexpr function parameters as template arguments

大兔子大兔子 提交于 2019-11-27 04:29:35
问题 I am playing around with some toy code using c++11 to figure out a bit more about how things work. During this I came across the following issue that simplifies down to: template <int x, int y> class add { public: static constexpr int ret = x + y; }; constexpr int addFunc(const int x, const int y) { return add<x,y>::ret; } int main() { const int x = 1; const int y = 2; cout << add<x,y>::ret << endl; // Works cout << addFunc(1,2) << endl; // Compiler error return 0; } I'm using GCC 4.8.1 and

constexpr not compiling in VC2013

我与影子孤独终老i 提交于 2019-11-27 04:10:31
This constexpr code does not compiled in Visual Studio 2013 version 12.0.21005.1 REL Is there a newer Visual Studio compiler that works with constexpr? #include <iostream> constexpr int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n - 1)); } int main(void) { const int fact_three = factorial(3); std::cout << fact_three << std::endl; return 0; } output from compilation: 1>------ Build started: Project: Project1, Configuration: Debug Win32 ------ 1> Source.cpp 1>....\source.cpp(3): error C2144: syntax error : 'int' should be preceded by ';' 1>....\source.cpp(3): error C4430: missing type

Populate An Array Using Constexpr at Compile-time

主宰稳场 提交于 2019-11-27 03:51:41
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 out which category an ASCII character belongs to. Something like char c = RandomFunction(); if (table[c

constexpr error at compile-time, but no overhead at run-time

可紊 提交于 2019-11-27 03:46:00
问题 There is a well-known trick to cause a compile-time error in the evaluation of a constexpr function by doing something like this: constexpr int f(int x) { return (x != 0) ? x : throw std::logic_error("Oh no!"); } And if the function is used in a constexpr context you will get a compile-time error if x == 0 . If the argument to f is not constexpr , however, then it will throw an exception at run time if x == 0 , which may not always be desired for performance reasons. Similar to the theory of

single expression helper for compile-time enforced constexpr function evaluation possible?

旧时模样 提交于 2019-11-27 03:05:34
问题 @cyberpunk_ is trying to achieve something and made some questions about it but all the chase boils down to this: Is it possible to build a tool to enforce compile-time evaluation of a constexpr function? int f(int i) {return i;} constexpr int g(int i) {return i;} int main() { f(at_compilation(g, 0)); int x = at_compilation(g, 1); constexpr int y = at_compilation(g, 2); } In all situations, at_compilation enforce compilation-time evaluation of g . at_compilation doesn't need to be in this

C++11 constexpr function pass parameter

霸气de小男生 提交于 2019-11-27 02:46:52
问题 Consider the following code: static constexpr int make_const(const int i){ return i; } void t1(const int i) { constexpr int ii = make_const(i); // error occurs here (i is not a constant expression) std::cout<<ii; } int main() { t1(12); } Why I have an error on make_const call? UPDATE But this one works: constexpr int t1(const int i) { return make_const(i); } However, this not: template<int i> constexpr bool do_something(){ return i; } constexpr int t1(const int i) { return do_something<make