The next version of the C standard (C23) brings two new features that I am not
enthusiastic about. One of them is the nullptr
constant, the other one is
the “new” auto
keyword1. I will try to explain why I don’t like it.
The new proposed auto
keyword will work basically like in C++. You will
for example be able to write:
auto x = 10;
auto ret = function();
Instead of:
int x = 10;
double ret = function();
According to the proposal the main use for this is to simplify macros with generic types. The example they give is this one:
#define dataCondStoreTG(P, E, D) \
do { \
auto* _pr_p = (P); \
auto _pr_expected = (E); \
auto _pr_desired = (D); \
bool _pr_c; \
do { \
mtx_lock(&_pr_p->mtx); \
_pr_c = (_pr_p->data == _pr_expected); \
if (_pr_c) _pr_p->data = _pr_desired; \
mtx_unlock(&_pr_p->mtx); \
} while(!_pr_c); \
} while (false)
This is a legitimate usage of the feature and I think it has some merit here
(though I don’t really understand why typeof
could not be used instead). The
issue I have is with the name: “auto” is too short and nice, and I am worried
that it will be used not just for macros, but everywhere.
C has the nice property that things that are hard to maintain are also hard to
write. The language gives you the tools to do all the dirty things you
sometimes need2, but always makes you pay for it, so that the incisive is to
keep the code simple. The new auto
keyword seems to go the opposit way:
making it easier to write code that is harder to maintain.
Instead of auto
I would have preferred if the proposal was to make GNU gcc
keywork __auto_type
the standard. That would still allows to write generic
macro code, but conveys the idea that this should not be abused.
This new feature will make C a little bit less pleasant to work with, and makes me feel like the people working on the language are trying to increase the compatibility with C++, which is of course a lost cause.
Not exactly new, the
auto
keyword was already in the standard with a different (and useless) semantic. ↩︎One new feature of C23 that I actually like is the #embed macro that has some clear use cases that were not possible before. ↩︎