C++

what are some good C++ tricks/quirks that only senior developers know?

You already invited:

juegas

Upvotes from: Innocent Magagula Brian

here’s my two cents… Knowing tricks/quirks doesn’t make you a senior developer, and being a senior developer doesn’t necessarily mean you know tricks/quirks.

IME, the mark of a good engineer (not necessarily C++, and not necessarily “senior”) is the ability to make use of and _judiciously_ apply the full breadth and depth of their tools in a way that solves the problem at hand while being (1) correct, (2) readable, and (3) efficient for its intended purpose. Yes, occasionally that means pulling out esoteric “tricks” from the bag. But those should absolutely be documented where used. And yes, that does mean everyone should continually strive to improve their knowledge of the tools they use and _how_ they can be used, including being fluent in multiple programming paradigms to be able to come up with a better solution that could be achieved with “pure OO”, “pure proceedural”, “pure functional”, or whatever else (this is particularly relevant to C++ as a multi-paradigm language).

Elaborating on “readable”, to my mind this doesn’t necessarily mean it is simplistic or that all of a solution is obvious at first glance, but rather that it’s appropriately documented and understandable in principle by anyone willing to put the time and effort into it, including the most junior of engineers on a project. That’s the “no haunted graveyards and no holy shrines” rule.

agrath

Upvotes from: Innocent Magagula

my favorite trick has to do with combining generic lambdas with parameter packs

so you could invoke your lambda with everything
speaking of which
why isn't std::invoke constexpr?

gitkiwi

Upvotes from: Innocent Magagula

There are a lot of dark corners and fiddly bits and unintuitive rules in C++, but most of them aren't practically useful, and most people have no reason to know them except for the sake of knowing all the quirks of the language.
So I guess, are you asking for practically useful stuff that relatively few people know about (like CRTP, SFINAE, _etc._), or are you asking for parlor tricks you can do to surprise people (like commutativity of subscripting), or are you just asking for random esoterica?

Eston

Upvotes from:

there are many new abstractions and things that make c++ easier and more performant without the requirement of you knowing the finer details

and this is great!
 
And the NVI idiom (I think of Herb Sutter when I think of this; did he invent it or just popularize it, or am I completely misassociating?) (edited) 

Ooh, for an example of a modern C++ "trick" that actually does make your code better and more clear and should always be used over the alternatives where possible, there's the Meyers singleton!

nanle

Upvotes from:

writing C instead is a pretty good trick

dan

Upvotes from:

Given how widespread its usage is, all of `<algorithm>` could be considered a "bag of advanced C++ tricks" :stuck_out_tongue:

(unfortunately)
There's also the thing where you use lambdas to wrap up multi-stage initialization (of constants, function parameters, or just things whose initialization you don't want to leak into the surrounding scope).
 
const int input = [] {
int in;
std::cin >> in;
return in;
} ();

If you wanna answer this question please Login or Register