3 ms·
R"(x)" literals are neat not just because whitespaces matter, but also because they are tokenized before macro expansion. Thus you can write a C23 detector lik
by omoikane 3y ago
R"(x)" literals are neat not just because whitespaces matter, but also because they are tokenized before macro expansion. Thus you can write a C23 detector like this:
#include<stdio.h>
#define r(R) R"()"
int main()
{
puts(r()[0] ? "C99" /* r() evaluates to "()" */
: "C23" /* r() evaluates to "" */);
}
Output: https://gcc.godbolt.org/z/Wj3s6KEGK https://gcc.godbolt.org/z/Wj3s6KEGK
I have used that trick here:
https://www.ioccc.org/years.html#2015_yang https://www.ioccc.org/years.html#2015_yang
(C23 wasn't a thing back then, but the same trick can be used to differentiate C++11 from C++98).
- rwbt 3y agoThat's clever!
- silasdavis 3y agoAnd that manages to be the most intelligible part of prog.c
- lifthrasiir 3y agoAnother easily recognizable part is: x*=02//* */2 ...which was used to differentiate C89 and anything later. (More detailed explanation, if you can speak Japanese or tolerate machine translation, is available on: https://mame.github.io/ioccc-ja-spoilers/2015/yang.html https://mame.github.io/ioccc-ja-spoilers/2015/yang.html )
- defen 3y agoThis is checking for the presence of raw string literals (A GNU C extension) not C23. If you compile with `-std=gnu99` instead of `-std=c99` you'll get "C23" as output.
- Sharlin 3y agoThe context is different standard versions. Random extensions don't count. C23 has raw string literals, C before 23 doesn't.
- ksherlock 3y ago> C23 has raw string literals Are you sure about that? I only see u, u8, U, and L defined as encoding-prefixes.
- Sharlin 3y agoEep, sorry. I misread the article.
- defen 3y agoNo it doesn't. If you don't specify a standard for GCC it uses GNU extensions by default.
- Sharlin 3y agoIndeed, my bad :/
- omoikane 3y agoMy bad, I just saw "R()" in the linked blog and thought the feature made it to C23, but looks like it's not standard. https://en.cppreference.com/w/c/23 https://en.cppreference.com/w/c/23 On the plus side, I now have a GNU extension detector.
- Karellen 3y ago> Thus you can write a C23 detector Can't you just check `__STDC_VERSION__` ? (Or, is your way a "But, where's the fun in that?" exercise? Actually, if you're an ioccc entrant, "where's the fun in that?" does suddenly become a leading hypothesis ;-)