TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25621 14
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
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Sep 29, 2026Boost.Graph 1.95 will be C++17 Dear Boost.Graph community, In two release cycles (Boost re…
  2. Sep 26, 2026Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C…
  3. Sep 25, 2026myStringStream.str("") Considered Harmful Under C++20 I was recently looking at some (rath…
  4. Sep 20, 2026A clever branch free optimization I'm the developer of memlz which is an extremely fast co…
  5. Sep 15, 2026Inside Boost.PolyCollection https://bannalia.blogspot.com/2026/09/inside-boostpolycollecti…
  6. Sep 11, 2026C++26: Standard Library Hardening Experiments https://www.cppstories.com/2026/hardening-ex…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →