C++ - Reddit
228 subscribers
48 photos
8 videos
25.3K links
Stay up-to-date with everything C++!
Content directly fetched from the subreddit just for you.

Join our group for discussions : @programminginc

Powered by : @r_channels
Download Telegram
fun fact about string literals i often forget:

String literals in c/c++ (const char*), support operator\[\] (in a compile time context)!

>constexpr char first = "test"[0\];
// i.e first = 't'

I think 90% of C developers would know this, but I figured maybe those of you who didn't start out with C might not have discovered this! I just recently used this in combination with an X macro for some debug ui code:

>// the x macro in question
\#define LIST_NOISE_PARAMS
X(heat)
X(rain)
X(cont)
X(grad)
X(hills)

I wanted to display the first letter of the noise parameter's name, in caps, followed by the noise value, i.e:

>H: 1.00
(the heat noise = 1.0f.)

>\#define X(VAR) UI::Text("{}: {:4.2f}",#VAR[0\]-('a'-'A'), sample.VAR);
LIST_NOISE_PARAMS
\#undef X

>// expands to:
UI ::Text("{}: {:4.2f}", "heat"[0\] \- ('a' - 'A'), sample.heat);
UI ::Text("{}: {:4.2f}", "rain"[0\] \- ('a' - 'A'), sample.rain);
// ...

>// which evaluates to:
UI ::Text("{}: {:4.2f}", 'H', sample.heat);
UI ::Text("{}: {:4.2f}", 'R', sample.rain);
// and so on for the rest of the list!

What other 'odd' C/c++ tricks/facts do you guys know/use? Personally, the X macro technique blew my mind when I first discovered it, and its probably the most legitimately useful 'trick' I know of. Pretty cool. Hopefully there are others in here that enjoy these little tidbits as much as i do.



https://redd.it/1uxwz1u
@r_cpp