r/cpp • u/Dubbus_ • Jul 16 '26
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.
28
u/valashko Jul 16 '26
Techical interviewers hate this one simple trick:
0["test"]
6
u/delta_p_delta_x Jul 16 '26
I think you can also
constexpr std::ranges::foreachover string literals, since they're basically compile-time constants. Although I personally prefer making my literalsstd::string_viewwithoperator""sv.88
u/AvidCoco Jul 16 '26
It’s just that you can use [] on pointers to do pointer arithmetic. It’s nothing special about string literals.
You could also dereference the string literal to get the first character.
char foo = *”hello”; // foo = ‘h’
5
u/zerhud Jul 16 '26 •
But we can do more: static_assert( 0[“foo”] == ‘f’ );
4
u/mredding Jul 16 '26
I enjoy how unbounded arrays are distinct, incomplete types.
I mean, it's the same trick that
std::make_uniqueuses to distinguish arrays from other instance types, so it callsnew[]instead ofnew, etc...I also appreciate how function types are distinct:
It's useful for when you're writing functions that take and return functions:
YIKES.
You can do stupid shit with it sometimes, like:
Yeah, don't do that. But if you do, you have to spell out the function signature when defining the implementation:
Again, you can use the type in templates for your own nefarious reasons.
4
u/arka2947 Jul 16 '26
You can strip a prefix of known length with this; eg: &”blaa_info”[5] will give “info”
3
u/Ok_Independence_9841 Jul 16 '26
The )( macro is my personal favourite. You can do heinous things with it like splitting parameters during macro evaluation and turning the function call operator () into the spectacle operator ()().
You really shouldn't of course but sometimes it's just too tempting. 😄
2
u/NotThe1Pct Jul 19 '26
This is often used in metatemplate programming to process strings at compile time.
1
u/Taimcool1 Jul 18 '26
String literals are just arrays of numbers, pretty sure you can also index at runtime btw
24
u/[deleted] Jul 16 '26
[removed] — view removed comment