Home > OS >  Is auto(x); considered a declaration of a variable named x or an explicit type cast in C 23
Is auto(x); considered a declaration of a variable named x or an explicit type cast in C 23

Time:05-08

I came across this which states:

Not a bug, auto(x); is interpreted as auto x;. Use auto(x); if you want that to be an expression.

The above seems to imply that since auto(x); is a declaration(equivalent to auto x;) it should be rejected since we're using auto and don't have an initializer.

While this states:

Yes this changed in C 23 so auto(X) creates an rvalue of the decayed type of x.


The above quoted statements seems to be contradicting each other. So my question is what does the C 23 standard say about this? I mean is auto(x); a declaration or a explicit type cast.

CodePudding user response:

Note the code referenced by the GCC bug in question:

int main() {
  int x = 0;
  float t;
  t = auto(x);
}

auto(x) here is not a statement; it is unequivocally an expression. If auto(x) is used as an expression, it will behave as an expression. If it however is used in a way that makes it a statement, then it will behave as such.

  • Related