4

I work on a large codebase that is migrating to C++14 (and eventually C++17 & beyond). We'd like to "constexpr all the things," but obviously with a large codebase, our team can't go in overnight and annotate the thousands of functions and variable declarations that really should be constexpr.

Is there a tool out there that can parse our code, figure out what could be constexpr, and automatically make the changes? I imagine this would require some human review... but any help we could get from tools would be a win.

I’d be thrilled with even partial solutions—e.g., turn all #defines of numeric constants into typed constant expressions.

(Note that my question is not why constexpr is not applied automatically by the compiler... instead, my goal is to modify the code to add constexpr wherever we can.)

Edited to add more info from the comments on my original SO post.

Q: Why would you want to do something like this?

Performance & safety. Ours is a long-lived project (the codebase has been in production for 30 years—first as C89—and we intend for it to live another 30 at least), so from time to time, we undertake modernization efforts like this when we think there's a big advantage to be had.

Q: Isn't this going to be a mess on a large product (introduce a massive diff, maybe introduce bugs, increase compile times, etc.)?

Maybe. But, if the transformation was automated, it'd be cheap to test the performance gain (maybe applying the change one subsystem at a time) and make a judicious decision about whether the it was worth the thrash of committing it. (Also, that's what tests are for!)

Q: Why not make the changes over time by hand?

This gets back to my motivation for wanting an automated solution—it'd be nice to know what the expected performance gains were before starting a months or years long project of doing it by hand... only to get to the end of it and discover it was a wash.

  • One partial solution, provided by Jason Turner: Current Visual C++ code analysis tools can tell you which functions can be constexpr, so that's a start. It's not fully automated. – s3cur3 Jan 29 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.