constexpr

C++1y/C++14: Converting static constexpr array to non-type template parameter pack?

泪湿孤枕 提交于 2019-11-30 15:34:59
Suppose I have a constexpr array (of known bound) of static storage duration: constexpr T input[] = /* ... */; And I have an output class template that needs a pack: template<T...> struct output_template; I want to instantiate output_template like: using output = output_template<input[0], input[1], ..., input[n-1]>; One way to do this is: template<size_t n, const T (&a)[n]> struct make_output_template { template<size_t... i> static constexpr output_template<a[i]...> f(std::index_sequence<i...>) { return {}; }; using type = decltype(f(std::make_index_sequence<n>())); }; using output = make

Function returning constexpr does not compile

不想你离开。 提交于 2019-11-30 12:36:48
Why doesn't this compile: Could there be a problem with a string as a return type? constexpr std::string fnc() { return std::string("Yaba"); } The constructor of std::string that takes a pointer to char is not constexpr . In constexpr functions you can only use functions that are constexpr . 来源: https://stackoverflow.com/questions/7779013/function-returning-constexpr-does-not-compile

constexpr question, why do these two different programs run in such a different amount of time with g++?

梦想的初衷 提交于 2019-11-30 12:33:17
I'm using gcc 4.6.1 and am getting some interesting behavior involving calling a constexpr function. This program runs just fine and straight away prints out 12200160415121876738 . #include <iostream> extern const unsigned long joe; constexpr unsigned long fib(unsigned long int x) { return (x <= 1) ? 1 : (fib(x - 1) + fib(x - 2)); } const unsigned long joe = fib(92); int main() { ::std::cout << "Here I am!\n"; ::std::cout << joe << '\n'; return 0; } This program takes forever to run and I've never had the patience to wait for it to print out a value: #include <iostream> constexpr unsigned long

g++ c++11 constexpr evaluation performance

此生再无相见时 提交于 2019-11-30 11:44:09
g++ (4.7.2) and similar versions seem to evaluate constexpr surprisingly fast during compile-time. On my machines in fact much faster than the compiled program during runtime. Is there a reasonable explanation for that behavior? Are there optimization techniques involved which are only applicable at compile-time, that can be executed quicker than actual compiled code? If so, which? Here`s my test program and the observed results. #include <iostream> constexpr int mc91(int n) { return (n > 100)? n-10 : mc91(mc91(n+11)); } constexpr double foo(double n) { return (n>2)? (0.9999)*((unsigned int)

Can constexpr function evaluation do tail recursion optimization

那年仲夏 提交于 2019-11-30 11:29:15
I wonder whether for long loops we can take advantage of tail recursion for constexpr in C++11? By the rules in [implimits] , an implementation is permitted to put a recursion depth limit on constexpr calculations. The two compilers which have complete constexpr implementations (gcc and clang) both apply such a limit, using the default of 512 recursive calls as suggested by the standard. For both of these compilers, as well as any other implementation which follows the standard's suggestion, a tail recursion optimization would be essentially undetectable (unless the compiler would otherwise

User defined literal arguments are not constexpr?

我怕爱的太早我们不能终老 提交于 2019-11-30 10:56:47
I'm testing out user defined literals. I want to make _fac return the factorial of the number. Having it call a constexpr function works, however it doesn't let me do it with templates as the compiler complains that the arguments are not and cannot be constexpr . I'm confused by this - aren't literals constant expressions? The 5 in 5_fac is always a literal that can be evaluated during compile time, so why can't I use it as such? First method: constexpr int factorial_function(int x) { return (x > 0) ? x * factorial_function(x - 1) : 1; } constexpr int operator "" _fac(unsigned long long x) {

Is constexpr-based computation Turing complete?

淺唱寂寞╮ 提交于 2019-11-30 10:44:24
问题 We know that C++ template metaprogramming is Turing complete, but preprocessor metaprogramming is not. C++11 gives us a new form of metaprogramming: computation of constexpr functions. Is this form of computation Turing-complete? I am thinking that since recursion and the conditional operator (?:) are allowed in constexpr functions, it would be, but I would like someone with more expertise to confirm. 回答1: tl;dr: constexpr in C++11 was not Turing-complete, due to a bug in the specification of

Constexpr vs macros

元气小坏坏 提交于 2019-11-30 10:33:44
问题 Where should I prefer using macros and where should I prefer constexpr ? Aren't they basically the same? #define MAX_HEIGHT 720 vs constexpr unsigned int max_height = 720; 回答1: Aren't they basically the same? No. Absolutely not. Not even close. Apart from the fact your macro is an int and your constexpr unsigned is an unsigned , there are important differences and macros only have one advantage. Scope A macro is defined by the preprocessor and is simply substituted into the code every time it

constexpr and std::cout working on function but not in lambda

六眼飞鱼酱① 提交于 2019-11-30 09:20:41
Why constexpr does not work with std::cout , but works with printf ? #include <iostream> constexpr void f() { std::cout << ""; } //error constexpr void g() { printf(""); } //ok And why std::cout works with lambdas constexpr ? #include <iostream> int main () { auto h = []() constexpr { std::cout << ""; }; //ok } Technically, it doesn't work with any of them. From [dcl.constexr] : For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a

constexpr initializing with pointers

梦想与她 提交于 2019-11-30 08:10:10
问题 I am trying to initialize a constexpr declaration with a pointer to int which is a const object. I also try to define an object with a object that is not a const type. Code: #include <iostream> int main() { constexpr int *np = nullptr; // np is a constant to int that points to null; int j = 0; constexpr int i = 42; // type of i is const int constexpr const int *p = &i; // p is a constant pointer to the const int i; constexpr int *p1 = &j; // p1 is a constant pointer to the int j; } g++ log: