Mr User
Mr User

Reputation: 265

C23 auto vs C++11 auto

The C23 standard apparently has introduced using "auto" keyword for auto type deduction, see here, just like in C++11. However, there seems to be some differences.

According to here, https://en.cppreference.com/w/cpp/keyword/auto, after C++11, auto is no longer a storage duration specifier in C++.

However, I cannot easily find an equivalently statement for C23. Is it the case that auto is still a storage class specifier in C in C23?

Can we still write int auto x = 1; in C23?

EDIT: The answer to the first question is yes. But as pointed out by Andrew Henle in comments below, the second question is different:

Can we still write float auto x = 1; in C23?

As quoted by @AndrewHenle and @VladfromMoscow, in the standard document, 6.7.1 Storage-class specifiers, paragraph 4

auto shall only appear in the declaration specifiers of an identifier with file scope or along with other storage class specifiers if the type is to be inferred from an initializer.

It seems that this does not cover the case float auto x = 1;, if this declaration is not in file scope.

What's the interpretation of this?

There is another question: the sentence seems confusing because we surely can use auto without "other storage specifiers", couldn't we? Like auto a = 1;.

Upvotes: 12

Views: 2933

Answers (3)

n. m. could be an AI
n. m. could be an AI

Reputation: 120059

float auto x = 1; is valid and means the same thing as in pre-C23 (and auto here is as redundant as in pre-C23). There is no indication in the working draft that it might not be the case.

auto shall only appear in the declaration specifiers of an identifier with file scope or along with other storage class specifiers if the type is to be inferred from an initializer.

This does not cover float auto x = 1; at a function scope. In this declaration, auto still specifies storage duration because:

Storage-class specifiers specify various properties of identifiers and declared features:

  • storage duration (static in block scope, thread_local, auto, register),

and this meaning of auto is not ignored because

If auto appears with another storage-class specifier, or if it appears in a declaration at file scope, it is ignored for the purposes of determining a storage duration or linkage

and auto does not specify that the type shall be inferred because

For a declaration such that the declaration specifiers contain no type specifier a mechanism to infer the type from an initializer is discussed in 6.7.9

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

In C23 auto is still a storage class specifier.

From the C 23 (6.7.1 Storage-class specifiers)

Syntax

1 storage-class-specifier:
    auto
    constexpr
    extern
    register
    static
    thread_local
    typedef

and

Semantics

6 Storage-class specifiers specify various properties of identifiers and declared features:

— storage duration (static in block scope, thread_local, auto, register),

4 thread_local shall not appear in the declaration specifiers of a function declaration. auto shall only appear in the declaration specifiers of an identifier with file scope or along with other storage class specifiers if the type is to be inferred from an initializer

Upvotes: 4

Ted Lyngmo
Ted Lyngmo

Reputation: 117822

Yes, auto is still a storage-class specifier in C23:

See 6.7.1 Storage-class specifiers:

  • auto
  • constexpr
  • extern
  • register
  • static
  • thread_local
  • typedef

Upvotes: 7

Related Questions