12 ms·
What's wrong with .cpp extension?
by aspyct 4y ago
What's wrong with .cpp extension?
- dale_glass 4y agoThe implication is that he's mostly writing C despite the file extension suggesting they contain C++. C++ changed a lot over time, and the way modern code is supposed to be written is very distinct from "C with classes", but you still can do that if you want.
- palata 4y agoWut? Makes no sense to me.
- dale_glass 4y agoHere's an example of C++ code written intentionally with all the latest C++ features: https://gist.github.com/caiorss/c7db87df674326793431a14006aa21f6 https://gist.github.com/caiorss/c7db87df674326793431a14006aa... It looks pretty much nothing like a C program doing the same job. A lot of those features were made to make C++ a safer language to use. Eg, with a construction like: for(const auto& it : ast){ You can't accidentally walk past the end of the array by going one item too far. But C++ still allows you to write code the way you'd do it in C.
- ithkuil 4y agoWhy some functions are declared like: void foo() { .. } And others auto foo() -> void { .. }
- pjmlp 4y agoFashion. The second syntax was introduced for lambdas and decltype() usage for return types that depend on the parameters (due to look up rules), and some C++ subcultures now use it everywhere.
- zoomablemind 4y ago>...And others > auto main() -> int { Absolutely no reason to masquerade main() in such a fashion! It's a rule of least surprise, be it for user or fellow developer.
- anacrolix 4y agoWhy are spaces randomly omitted for language constructs like if and for, and reference type &s left aligned?!
- ralferoo 4y agoRandomly? This author does it consistently. Some people prefer with spaces, some prefer without, most people follow the coding standards for the company / project they're working on.
- kfajdsl 4y agoWhat's wrong with left aligned & and *? I prefer it because for me it makes more sense; it is not a <type>, it is a <type> reference (or pointer). This involves different semantics than just a <type>, so it makes sense to me for the extra information to be with the type.
- mannykannot 4y agoIn other words, you are not aware of anything wrong with this code.
- ofrzeta 4y agoThat's quite readable and actually makes me wanna try C++20 or whatever it is.
- synergy20 4y agothe gist is mostly c++11, all I can say is that c++20 is much better. with constexpr etc, and std::move, and unique_ptr in use, many memory safety issues can be contained. adding cppfront on top can make modern c++ even more memory safe.
- zoomablemind 4y ago>...Here's an example of C++ code written intentionally with all the latest C++ features. Does it really need to include <string.h>?
- amluto 4y agoSure, but it’s not enough. You can forget the virtual destructor. Maybe the language is excused because it’s only a leak. You could mess up the what() functions. (It’s not locally obvious that it’s correct in this code.) std::exception is fundamentally dangerous. Rust can get away with safely returning references like that because the lifetime is part of the function’s signature. C++, even modern C++, can’t. Even the iterative example is only a bit safe. You can’t walk off the end by going one too far, but you can certainly mess up by invalidating the iterator in the loop body.
- cesarb 4y ago> You can't accidentally walk past the end of the array by going one item too far. AFAIK, that's with a caveat of "unless you modified the array length within the loop" (looking at the gist, "ast" is a deque, so you can pop items from either end); IIRC, that C++ construct evaluates the begin and end only once at the start, instead of every iteration (unlike what you might do with "classic" C++ evaluating "it != ast.end()" every time), so it wouldn't notice the change. This is particularly annoying since C++ developers can be tempted to optimize a classic "for (auto it = ast.begin(); it != ast.end(); ++it)" loop into either the new-style "for (auto it : ast)" or the old-style optimized "auto end = ast.end(); for (auto it = ast.begin(); it != end; ++it)" loop, even though that would break the code if "ast.end()" ever changes within the loop. (Since the initial discussion is actually about C and C++ versus Rust: in Rust, you wouldn't be able to modify the array within the loop, since the iterator is borrowing it, so this issue would be caught by the compiler.)
- dannymi 4y agoThanks for the example! Checks linked resources while(!ss.eof()){ ss >> token; Can cause an endless loop (with finite input stream; for example in case of a read error). p >> x; if(!p.fail() && p.eof()){ This way of error checking is correct for C++ but not nice (it or part of it can be forgotten with no warning--see below for an instance of it :P). struct stack_empty_error: public std::exception{ const char\* what() const throw(){ Missing "override" annotation here. Can easily fuck it up since overloads are allowed. return " ==> Error: stack empty." ; } }; auto x = _stack.back(); _stack.pop_back(); return x; Correct in C++ but incredibly weird--typical for lowlevel languages that are far away from the user. Also, when I press Ctrl-D, I get an endless loop printing "EXPR+> stack". Typical C++ program...
- nindalf 4y agoThey’re claiming the CTO of Azure doesn’t know enough about modern C++ to make the statement that we’re discussing. Bold claim.
- mkl 4y agoI find it hard to imagine the CTO of Azure has done enough C++ programming in the last few years to be fluent in C++20. Maybe as a hobby I suppose; it doesn't match my understanding of what a CTO of a big organisation does otherwise.
- nindalf 4y agoHe doesn’t need to be fluent in every language to set technical direction as a CTO. You don’t question a general’s war strategy simply because they aren’t accurate with a rifle.
- forgotpwd16 4y ago>You don’t question a general’s war strategy simply because they aren’t accurate with a rifle. Thing is in this case their advice regards the rifle.
- nindalf 4y agoRight. So the general says we should use a different weapon. That’s fine. It’s also fine to question if that strategy makes sense. It is nonsensical to say “the general can’t hit a moving target with the old rifle, so he should say nothing at all about weapons”. Which is what the person I replied to said.
- philippejara 4y agoThe point the person you replied to was making was that him being CTO didn't add much credibility to his claim as you seem to assume by bringing it up, not that it necessarily removed credibility.
- KptMarchewa 4y ago>the way modern code is supposed to be written Many experts disagree with the way modern C++ evolves. To say that modern code is supposed to use all the C++11-forward features or else it's obsolete is a massive bias on your part.
- galangalalgol 4y agoC with classes originally meant non oo c++. It was a common crticism from a time when not being oo was considered a defect rather than a design decision on which paradigm fit your problem space. It would be like a haskell zealot saying someone's clojure was "java with lambdas" to imply it was imperative. Implying imperative code was always inferior. All that said, some parts of moder c++ like move semantics do really cut down on defects. People who go out of their way to use *all* of the new toys create codebases that make my teeth itch.
- KptMarchewa 4y agoThe "classes" part explicitly means OO. It meant not using stuff like templates, operator overloading, RTTI, exceptions, STL.
- sph 4y agoSpeaking of which, why do .cc and .cpp exist to refer to C++ source files? Two compiler vendors that couldn't agree?
- blep_ 4y agoIt's worse. Others include .cxx (fairly common) and .C (thankfully rare).
- cesarb 4y agoYou forgot .C (yes, uppercase C means C++, lowercase c means C), .c++ (the best one IMO), and .cxx (when your filesystem doesn't accept crosses, you tip them over). See https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Invoking-G_002b_002b.html https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Invoking-G_002... for the full list.
- meragrin_ 4y agoC is not C++. While they strive for compatibility, I'm pretty sure there are cases where code compiled under a standards conforming C compiler will act differently under a standards conforming C++ compiler. Most C/C++ developers would compile a .cpp file with a C++ compiler if no other information was available.