Just letting folks know that I've posted the next article in my series on C++11, this one covering constexpr and generalized constant expressions.
Feedback, as always, is very much appreciated.
Just letting folks know that I've posted the next article in my series on C++11, this one covering constexpr and generalized constant expressions.
Feedback, as always, is very much appreciated.
The only thing I can find wrong in the article is this:
This is obviously not true, since constants such asIf you had wanted to do this in C++ without a constexpr, you'd have needed to create a macro since you can't use the result of a function call to declare an array.
const int n = 30;
is a constant expression and can be used to initialize things such as arrays.
Templates can also be used to simulate compile-time functions.
The function you wrote could be written easily with templates, as well.
That is not to say that templates are preferred. I'd say they should be avoided if only they obfuscate code. So there is one advantage constexpr has. Otherwise, I find the article good.
I guess I'm not getting the argument, but macro functions look like functions, even if they aren't called like one.
I wonder if levels of nesting and recursive constexpr function invocations (wording in the standard) mean the same thing? A naive fibonacci function doesn't go deep, but it sure has lots of invocations.The standard allows compilers to limit the levels of nesting allowed for recursive constexpr methods (although it does require at least 512 levels to be allowed). The reason for this limitation is that it allows compilers to detect and prevent some performance issues (preventing compiles that take an exponential amount of time due to a naive implementation of the fibonacci sequence, for example).
For example, a naive fibo(512) would have 512 levels of nesting, but would take eons to compute due to the amount of invocations. OTOH, if we were to count number of function calls, then fibo(12) may be the largest fibonacci number we can compute.
Or perhaps the compiler should apply memoization, and indeed allow us to go 512 levels deep?
Last edited by anon; 09-28-2011 at 09:11 AM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
@Elysia The point of the sentence you're talking about is to make the distinction in the case of wanting to compute the size of the array using some kind calculation, rather than simply using a constant. You can't do that with just a constant; you need a macro or (and this is a genuine omission for sure) some template metaprogramming. I've added a reference to template metaprogramming as another potential solution here.
@anon Whoops! That's a very good point; my example is definitely wrong, and not talking about memoization is a definite omission. fib( 500 ) or so compiles fine and fast on GCC 4.7, using doubles (integers overflow). I also found this patch note that indicates GCC explicitly memoizes, which matches my experiment. I've updated this section of the article to correct the error and discuss memoization.
O_o
To be Joe, I think you've missed a huge feature of `constexpr'.
I can make the generics mechanism do everything in the article using simple portable facilities.
You need a strong example of an iterative (as recursion) function using floating point mathematics. (I'm not talking about a simple constant expression like the area example; I'm thinking something more along the line of a function that must be accumulated in some fashion. A function approximating sine or something similar.) That is something that simply can't be done using portable facilities of the generics mechanism. The developer would have to jump through a lot of hoops to get such a facility. (Going so far as faking floating points manually with bit fiddling on certain compilers.) Not only is the `constexpr' version clean, it is a simple recursive translation; you don't need to understand the generics facilities at any level to comprehend the `constexpr' version.
With that sort of example, the feature isn't just a cleaner way to express a facility, it offers entirely new possibilities.
Soma
That's great feedback. I will work on an update to the article that includes more information on compile time floating point calculations that includes some kind of approximation example.