Madagascar
Madagascar

Reputation: 7345

Restrictions on auto keyword for type inference in C2X

int main()
{
    auto status = 203;
    auto static const str = "Hello";
    auto static const strs = {"Hello", "World"};
    return status;
}

From playing around with auto (and reading some articles), I discovered that it can not be used as a function's return type or as a parameter, but can only be applied on objects.

It works just fine for the first two statements in the above code, but fails for strs with:

auto.c: In function ‘main’:
auto.c:5:30: error: expected expression before ‘{’ token
    5 |     auto static const strs = {"Hello", "World"};
      |                              ^
compilation terminated due to -Wfatal-errors.

Why can't the type of strs be inferred? What other restrictions are there for auto (except for the two I have mentioned above)?

Upvotes: 0

Views: 58

Answers (0)

Related Questions