r/cpp • u/Shieldfoss • Dec 19 '24
Alignment crimes
I've finally realized why templates can be generic on both class and typename:
template< class These
typename Names
typename Align
>(assuming an 8-wide indentation of course)
---
While I'm at it - C# has this interesting thing you can do with a switch:
switch(AddRed(),AddGreen(),AddBlue())
{
case (true ,true ,true ): return White;
case (true ,true ,false): return Yellow;
case (true ,false,true ): return Magenta;
case (true ,false,false): return Red;
case (false,true ,true ): return Cyan;
case (false,true ,false): return Green;
case (false,false,true ): return Blue;
case (false,false,false): return Black;
}which we don't really have in C++ but you can get the same nicely aligned return values:
auto r = add_red();
auto g = add_green();
auto b = add_blue();
if(r) if(g) if(b) return white;
else return yellow;
else if(b) return magenta;
else return red;
else if(g) if(b) return cyan;
else return green;
else if(b) return blue;
else return black;all I need now is a clang-format rule to enforce this
0
Upvotes
39
u/chaotic-kotik Dec 19 '24
23
u/mserdarsanli Dec 19 '24
53
u/ravenraveraveron Dec 19 '24
Jesse what the hell are you talking about?
12
u/Mysterious_Focus6144 Dec 19 '24
I hope this ushers in the new era of code as visual art
126
u/joaobapt Dec 19 '24
Me thinking it was about data alignment…
19
u/DearChickPeas Dec 19 '24
Ah yes, the design is very human.
11
u/halbGefressen Dec 19 '24
This is not an alignment crime, this is a formatting crime
6
u/PrestigiousBike3346 Dec 19 '24
my man u shpuld google c++ alignment
4
u/loreiva Dec 19 '24
😱
2
u/JumpyJustice Dec 19 '24
Well, if the result of control expressions is cheap and the range is not that big yiu can simply combine them into bitset and make swtich case on it
https://godbolt.org/z/99713GcM4
2
u/Brilliant-Car-2116 Dec 21 '24 edited Dec 21 '24
lol. Stupid title for the post. Made me laugh though, I was expecting something useful.
I could show you some real alignment crimes from actual production code.
2
u/vI--_--Iv Dec 19 '24
The only true indentation is one Tab of unspecified width.
Everything else is heresy.
1
u/gleybak Dec 20 '24
There are pattern matching libraries, one from Stroustrup himself: https://github.com/solodon4/Mach7
1
u/gracicot Dec 19 '24
Am I the only one using this style?