Like Tree2Likes
  • 2 Post By phantomotap

Constexpr and generalized constant expressions in C++11

This is a discussion on Constexpr and generalized constant expressions in C++11 within the C++ Programming forums, part of the General Programming Boards category; Just letting folks know that I've posted the next article in my series on C++11, this one covering constexpr and ...

  1. #1
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    970

    Constexpr and generalized constant expressions in C++11

    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.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    The only thing I can find wrong in the article is this:
    If 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.
    This is obviously not true, since constants such as
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by Elysia View Post
    This is obviously not true, since constants such as
    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.
    I think that the article clearly says about function calls, not template tricks, which obviously cannot return anything at compile-time.
    I never put signature, but I decided to make an exception.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    Clearly, the article omits the facts that it can be done some other way. A macro isn't a function, either, so that kind of "fails."
    Regardless, I do believe it should be clear that there are other ways to do it, some not so optimal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    I guess I'm not getting the argument, but macro functions look like functions, even if they aren't called like one.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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).
    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.

    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    970
    @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.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,243
    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
    Fordy and webmaster like this.

  9. #9
    Administrator webmaster's Avatar
    Join Date
    Aug 2001
    Posts
    970
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. generalized recursive for loop
    By moddinati in forum C Programming
    Replies: 3
    Last Post: 12-18-2008, 03:58 PM
  2. Typesafe, generalized aliasing
    By CodeMonkey in forum C++ Programming
    Replies: 15
    Last Post: 12-13-2008, 08:52 PM
  3. ideas for a generalized unit-handling class
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 06-19-2008, 09:42 AM
  4. What's a good generalized data structures book?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-16-2006, 12:01 PM
  5. generalized matrix inversion program
    By ashesh in forum C Programming
    Replies: 1
    Last Post: 08-13-2002, 11:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21