4 ms·
Articles like this make me feel very smug about my decision never to go down the rabbithole of learning C++. I feel perfectly comfortable with using pointers (
by meuk 8y ago
Articles like this make me feel very smug about my decision never to go down the rabbithole of learning C++.
I feel perfectly comfortable with using pointers (and my intuition about them) in C.
- chubot 8y agoFWIW I mostly use “out params” as in C because I think moves incur a lot of complexity, as well explained in this article. It’s also more lines of code to write. Some people may quibble with it but I think it’s still a fine style of writing C++, and in fact most C++ you’ll see is written like that, since this is a new-ish feature.
- strictfp 8y agoAgreed. RVO is not guaranteed, and move is complex. Smart pointers have overhead. Out params are for the most part a sensible choice in C++. Sadly. I'm honestly shocked that this problem hasn't been addressed in a better way.
- RcouF1uZ4gsC 8y agoRVO is guaranteed in C++17
- ncmncm 8y agoOut-parameters are a terrible choice in C++. If it matters whether RVO is guaranteed, you are probably making something more complicated than it needs to be. Value semantics give you sensible behavior. Smart pointers should not appear in user-visible interfaces. It is easy to tie yourself in knots by doing the complicated thing. The solution is to do the simple thing.
- notacoward 8y ago> It is easy to tie yourself in knots by doing the complicated thing. The solution is to do the simple thing. Good advice. Corollary: don't use tools that make it hard to do simple things, or force you to live with the not-simple things others have done.
- ncmncm 8y agoMy available choices for what I do are C++ and, theoretically, Rust. Rust has the advantage of fewer legacy choices thrust upon it, although it is rapidly building up its own legacy. It is thus far less expressive, so I cannot capture as much meaning in a library. It is less mature, limiting its practical usability. It has some complications C++ lacks, some of which are unavoidable even in little programs. By the time Rust is mature, it (including its milieu) will be as complicated as C++ is today, with as many legacy boat anchors. But it will be as expressive and as practically useful, and I will have two choices of commensurate standing. For now, C++ is really all we have. It all traces back to the destructor: the original innovation that makes the rest possible. Rust's Drop trait fills the identical role. Nothing else has it. How do I deal with C++'s complexity? I stay the hell away from things that are too hard to understand. It's not hard. The most useful parts of the language are simple when used right.
- leetcrew 8y ago> Out-parameters are a terrible choice in C++ why? I personally love functions like `EResult function(const T1& inParam1, T2& outParam2)`. the function signature makes it obvious what the function does (or should do, at least) and it's easy to avoid accidental copying.
- ncmncm 8y agoCode like that is the bane of my existence. Give me `T2 function(T1)` every time. Besides being overwhelmingly clearer, I can use it in just one line. The other needs at least 3, more commonly 5, lines packed around it, so less than 20% of your code actually does anything useful toward the problem it is supposed to solve. Five times longer means you have five times the bugs. Besides being horrible style, operating on references cripples the optimizer. "Avoid accidental copying"? What good is that if it's slower than if you did the copy?
- signa11 8y agoserious question: would you throw an exception if T2 cannot be created ? or use a variant data-type ? or something else altogether?
- bigcheesegs 8y agoUse a variant type like llvm::Expected.
- signa11 7y agoyup, that's what i thought. i generally tend to use a construct simiar to 'error_or' though. that wrapped within a macro (^^) makes the whole invokation quite bearable
- ncmncm 7y agoGenerally, yes, throw. Sometimes an optional is better, such as when downstream already takes one. Occasionally, checking whether the operation (and a bunch of others) would succeed is best, particularly in high-rel/high-av systems. There, you are probably also logging, first, what you are about to attempt.
- tomnj 8y agoC++ is for sure extremely complicated, but I’d argue that a full understanding of value categories (glvalues, xrvalues, etc) isn’t necessary to be productive with the language.
- xsmasher 8y agoI spent five years in the C++ mines, and spooky corners and edge cases and arcane rules like this made me happy to leave. Working in a language where I can be concerned with the task and hand and not ownership issues is refreshing.
- userbinator 8y agoTo me, knowing C++, especially the "modern" dialect, is good for job security but like you I prefer plain old C for everything else.