5 ms·
The C++ Iceberg
- bun_terminator 3y agoA lot of classic c++isms, some good ones. constexpr weirdness is among my top choices
- WalterBright 3y ago> A good way to think about constant expressions is that `constexpr` is a hint that lets the compiler know that the variable or function CAN be evaluated at compile time, but it is not required In D, CTFE (Compile Time Function Evaluation) is guaranteed to run at compile time. CTFE happens whenever there's a constant-expression in the grammar, such as the initialization of an enum: enum A = func(3); func(3) evaluates at compile time.
- bingo3131 3y agoCalling it a hint is very much over-simplifying things as there are also restrictions on what a constexpr function is and is not allowed to do, although those restrictions become fewer and fewer as time goes on as compilers are so advanced that they can emulate more system level things at compile time. Also, constexpr on a variable is not a hint - it must be evaluated at compile time, where-as a constexpr function will be evaluated at either compile time or runtime depending on the context and arguments passed to it. For a function that you either want to work at compile time or not at all (compile error), consteval is the hardened version. Yes, sadly this means "constexpr" variables and "consteval" functions must be calculated at compile time, while "constexpr" functions can be used both at compile time and runtime. An annoying distinction.
- WalterBright 3y agoIn D, any function can be used to evaluate at compile time. If it is used for a constant-expression and cannot be evaluated at compile time, it is a compilation error. Also, the entire function needn't be evaluatable at compile time - only the path taken through it. Simply no need for tagging them one way or another.
- adrian_b 3y agoI agree, but the "constexpr" weirdness links to a video with a clickbait title. You are expected to watch most of the video, before learning that the purpose of the video is to argue that in almost all cases one should use "static constexpr" and not just "constexpr". The argumentation can be interesting, but it would have been nicer to just state the conclusion from the beginning, allowing a decision of whether it is desirable to spend time to also know the reasons for this.
- camel-cdr 3y agoI love how crazy C and C++ can be sometimes. We did a C preprocessor meta programming iceberg a while back: https://jadlevesque.github.io/PPMP-Iceberg/ https://jadlevesque.github.io/PPMP-Iceberg/
- DonHopkins 3y agoThe PHP Hammer would be the perfect tool to chip away at the C++ Iceberg with! https://blog.codinghorror.com/the-php-singularity/ https://blog.codinghorror.com/the-php-singularity/
- mgaunard 3y agoinline definitely means inline, as in the definition is provided in-line with the declaration. People are just confusing it with the inlining optimization that a compiler might do.
- tylerhou 3y agoIt’s more precise to say that inline means “this definition might be duplicated.” Non-inline functions also are allowed to have their definitions provided inline with the declaration; the difference is that the linker will error if it encounters a duplicate non-inline definition.
- josemanuel 3y agoAre you thinking of "weak" rather than "inline"? I always thought of "inline" as a hint to the compiler -- the compiler can inline, but it may not, depending on some compiler-specific optimisation objective (execution speed, code size, ...).
- mgaunard 3y agoYou thought wrong. Marking the symbol as weak is what inline indeed does from a technical point of view.
- tylerhou 3y agoNo, I am not thinking of weak; this is why “inline does not mean inline” is on the iceberg :). Although practically speaking inline is also used as a hint. But chiefly it refers to inline linkage. I’m actually also not familiar with weak, but a quick glance suggests that it might not mean the same thing as inline. Can weak symbols be defined multiple times? EDIT: yes, according to an SO post compilers often use weak to implement inline.
- mgaunard 3y agoI fail to see how it's more precise. Obviously if the definition is inline, it's duplicated in every point where the declaration is imported. Beginners fail to understand for example that providing a class member function definition within the class definition makes it inline, but that's already what you're doing textually, and that's what the keyword means.
- dailykoder 3y agoDamn, I should write more C++ again. I am missing so much fun. (Edit: I am not sure myself if I mean this sarcastic or not)
- fransje26 3y agoA form of Stockholm syndrome, perhaps? ;-)
- Deeg9rie9usi 3y agoDupes: https://news.ycombinator.com/item?id=40273141 https://news.ycombinator.com/item?id=40273141 https://news.ycombinator.com/item?id=39384093 https://news.ycombinator.com/item?id=39384093
- H8crilA 3y agoThe most terrifying concept is still the good old undefined behavior. How is it possible that a simple `gets()` call can pop a reverse shell and empty your bank account? Or steal national secrets and potentially cause a war? Undefined truly means undefined, though few people realize the scope at first when they read those innocent sounding words: The behavior is undefined
- flohofwoe 3y agoThe UB mess only started when compiler writers decided it is ok to exploit UB for optimisations which then leads to wildly unpredictable behaviour when you actually hit UB. But also: turn warnings to 11, use UBSAN, ASAN, TSAN and static analysers. These are all easy steps to make C and C++ code a lot more robust.
- H8crilA 3y agoNo, UB has always been just undefined. `gets()`, one of the most exploited functions in the history of computers, does not require any clever malice from the compiler. And there are a lot of such examples. In fact practice has shown that it has always been very, very hard to write a program that cannot reach UB. Thinking that you know what `undefined` really is results in undefined behavior.
- Xeamek 3y agoIt's not like UB is some magic functions that gets called when encountering UB. Every UB could verry well be wrapped in error checking and we could have 0 UB out there. We don't do that, but that's because of (our) choice, not some superficial entity that poses machine when running into UB.
- gpderetta 3y ago> Every UB could very well be wrapped in error checking Not in C and C++. How would you check that a pointer is safe to dereference for example?
- yu3zhou4 3y agoIt would be great to get an explanation for each of items here for non-C++ devs
- QuackyTheDuck 3y agoJust click on a term to be redirected to an explanation.
- axegon_ 3y agoKudos to whoever made this. No, really, thanks for making me laugh, I needed this.
- ben-schaaf 3y agoThere's also the Static Initialization Order Fiasco.
- bingo3131 3y agoFor those who missed it: each item on the iceberg is actually a link that explains the topic in more detail.
- fsloth 3y agoPopularized articles about C++ are very hit or miss. It’s partly because the language is so hit and miss in it’s non-design. The most glaring error in the iceberg IMO is the claim that shared_ptr is an antipattern. Shared_ptr is brilliant for immutable data in parallel programs. It’s not an antipattern. It’s useless for sure as a building block for data structures and other linked data with potential for circular references. But really, if a resource is a) immutable and b) needed in a shared context then a thin wrapper around a map of shared pointers or such saves the pain of needing to implement a much more complex resource management scheme.
- BenFrantzDale 3y agoAs a C++ user, shared_ptr is great for some things, but it is an anti-pattern. Shared_ptr<const T> is much much better. The problem is that shared_ptr<T> isn’t value-semantic and so destroys local reasoning. That said, there are places for it, but it’s very easy to make a mess with it. I’m a huge gain of stlab::copy_on_write<T>, which is fundamentally very similar but which is value-semantic, doesn’t let you make loops, and gives You local reasoning.
- bmoxb 3y agoI don't see why std::optional being a monad is implied to be a bad/confusing thing? It would be significantly more awkward to use without any monadic operations on offer.
- jrpelkonen 3y agoMaybe I’m missing a joke here but to me “godbolt is a real person” seems way out of place. Compiler Explorer is a great contribution to the community and he gives informative and humorous presentations. I would like to know why he got dragged into this list.
- bingo3131 3y agoA lot of programmers refer to the compiler explorer as Godbolt due to the domain name, such as saying "Godbolt link", "try it on Godbolt", etc. They do not realise that the domain name is Godbolt.org not because "Godbolt" is the name for the compiler explorer, but that the creator/maintainer's name is... Matthew Godbolt. I personally found out the domain was named after the creator when I saw a CppCon talk about the explorer from him!
- linhns 3y agoNo seriously, my colleagues when I told them though he was a comic book character.