constexpr

Guidelines to do constexpr operator-overloading?

爱⌒轻易说出口 提交于 2019-11-30 00:43:14
Consider a simple int Wrapper class with overloaded multiplication operator*= and operator* . For "old-style" operator-overloading, one can define operator* in terms of operator*= , and there are even libraries like Boost.Operators and its modern incarnation df.operators by @DanielFrey that reduce the boilerplate for you. However, for compile-time computations using the new C++11 constexpr , this convenience disappears. A constexpr operator* cannot call operator*= because the latter modifies its (implicit) left argument. Furthermore, there is no overloading on constexpr , so adding an extra

Template tricks with const char* as a non-type parameter

时光怂恿深爱的人放手 提交于 2019-11-30 00:11:29
I am very well aware that passing directly a const char* as a template non-type parameter is erroneous, since two identical string literals defined in two different translation units may have different addresses (although most of the time the compilers use the same address). There is a trick one may use, see code below: #include <iostream> template<const char* msg> void display() { std::cout << msg << std::endl; } // need to have external linkage // so that there are no multiple definitions extern const char str1[] = "Test 1"; // (1) // Why constexpr is enough? Does it have external linkage?

Whyever **not** declare a function to be `constexpr`?

不羁的心 提交于 2019-11-29 22:47:08
Any function that consists of a return statement only could be declared constexpr and thus will allow to be evaluated at compile time if all arguments are constexpr and only constexpr functions are called in its body. Is there any reason not to declare any such function constexpr ? Example: constexpr int sum(int x, int y) { return x + y; } constexpr i = 10; static_assert(sum(i, 13) == 23, "sum correct"); Could anyone provide an example where declaring a function constexpr would do any harm? Some initial thoughts: Even if there should be no good reason for ever declaring a function not

Is constexpr-based computation Turing complete?

半城伤御伤魂 提交于 2019-11-29 22:08:56
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. Richard Smith tl;dr: constexpr in C++11 was not Turing-complete, due to a bug in the specification of the language, but that bug has been addressed in later drafts of the standard, and clang

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

僤鯓⒐⒋嵵緔 提交于 2019-11-29 21:13:42
问题 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...

constexpr: definition and declaration for constexpr members

五迷三道 提交于 2019-11-29 18:08:19
If I want to use some convenience stuff like make_array I have no chance to declare my array first and later make the definition as done in "earlier" times because the type of my var is not available before definition. So I found this answer: Undefined reference to static constexpr char[] In the following example I wrote this solution which compiles fine with gcc and I am not sure that this is really valid c++ code, because it is more or less a declaration with definition and later a definition without any content. Is this allowed? ( Compiles fine is not a guarantee that the code is valid c++

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

假装没事ソ 提交于 2019-11-29 17:59:09
问题 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

Function returning constexpr does not compile

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 17:58:19
问题 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"); } 回答1: 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

g++ c++11 constexpr evaluation performance

老子叫甜甜 提交于 2019-11-29 17:25:16
问题 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

Can constexpr function evaluation do tail recursion optimization

帅比萌擦擦* 提交于 2019-11-29 17:00:43
问题 I wonder whether for long loops we can take advantage of tail recursion for constexpr in C++11? 回答1: 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