Home > Backend Development > C++ > Why Doesn't C 11 Support Designated Initializers Like C99?

Why Doesn't C 11 Support Designated Initializers Like C99?

Mary-Kate Olsen
Release: 2024-12-12 16:01:09
Original
590 people have browsed it

Why Doesn't C  11 Support Designated Initializers Like C99?

C 11's Exclusion of Designated Initializers: The Committee's Rationale

In C99, designated initializers offer a convenient way to initialize specific members of a struct, as illustrated by the following example:

struct Person {
    int height;
    int weight;
    int age;
};

int main() {
    Person p = { .age = 18 };
}
Copy after login

However, in C 11, this syntax is not valid. The C 11 standard committee has repeatedly rejected proposals to include designated initializer support, despite its practicality in C99.

Reasons for Exclusion

The committee's primary concern stems from the indeterminate order of evaluation for subexpressions in C99 designated initializers. Consider the following example:

struct X {
    int c;
    char a;
    float b;
};

struct X foo = { .a = (char)f(), .b = g(), .c = h() };
Copy after login

In C99, the order in which these function calls (f(), g(), and h()) would execute is undefined. This ambiguity could lead to indeterminate program behavior if these functions interact or have side effects.

C 's Stringent Initializer Requirements

In contrast to C99's flexible evaluation order, C mandates a strict sequential evaluation of initializer-clauses. As a result, implementing designated initializers in C would have required a different behavior:

f();
g();
h();
Copy after login

This would have broken compatibility with existing C99 implementations.

Proposed Solutions and Limitations

The c 20 standard partially addresses this issue with the introduction of P0329R4, which allows limited support for designated initializers. However, this support is restricted to avoid the complexities and potential pitfalls associated with indeterminate evaluation order.

Conclusion

The C 11 standard committee's decision to exclude designated initializers was driven by concerns about maintaining a consistent and predictable initialization process. While Boost provides an implementation for designated initializers, the standard committee has emphasized the challenges of integrating such a feature into C without introducing complexities or breaking compatibility.

The above is the detailed content of Why Doesn't C 11 Support Designated Initializers Like C99?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template