CPP: Fibonnaci -> More idiomatic C++ code#367
Conversation
|
Thanks!
The C version is considered reference for this. So that means we rather want all contributions to do the two checks. At least until we decide to change the reference. This is not consistently applied so far, but anyway, please keep the two checks for now. |
|
** DO NOT MERGE ** I've undone the if() checks. |
|
Ok, I ran the tests again on my super slow notebook to take some measurement noise out of it. It's very close and certainly in line with the C implementation. I'll leave it up to you guys whether a few ms regression is worth it to showcase the actual language differences vs. just using the same program with a different compiler. Not sure what's more important here TBH. Happy to abandon, happy to have it merged now. Your call. Thx either way. |
PEZ
left a comment
There was a problem hiding this comment.
I think we can't have constexpr in there.
Except for |
|
[[constexpr]] is a suggestion to the compiler, not a rule. It means this function can be invoked in a constexpr context, not that it will. In this example, since the arguments to the function are a command line argument, it clearly cannot be evaluated at compile time. Further more, the before and after benchmark results show that this has no effect. Again, just showcasing language features here. Just let me know. |
Changes from the C-based version: - Added command line argument count check - C++ does not require "return 0" in the main function - Trailing return types - Convert argv/argc to std::span to void pointer arithmetic and allow for simpler iteration (if needed) - C++23 provides std::print() and std::println() to replace std::cout and friends - C++23 ranges and algorithms to clarify intent and to avoid raw for loops. See Sean Parents "No raw for loops" and other sources.
|
Removed constexpr. |
|
Thanks for improving this project, @int2str! 🙏 And thanks for removing |


Changes from the C-based version:
Minor performance tweak:
Note:
The performance tweak is also applicable to the C version.
Benchmark:
