Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Designated initializers are not valid in C++11 #116

Closed
guilhermeferreira opened this issue May 30, 2017 · 2 comments
Closed

Designated initializers are not valid in C++11 #116

guilhermeferreira opened this issue May 30, 2017 · 2 comments

Comments

@guilhermeferreira
Copy link
Contributor

guilhermeferreira commented May 30, 2017

In some test cases, there are some member initializations like this:

MQTTAsync_successData data = {
	.token = MESSAGE_ID,
};

This feature is called designated initializers. It is an addition to C99 but left out of C++11. According to The C++ Programming Language, 4th edition, section 44.3.3.2 (C Features Not Adopted by C++):

A few additions to C99 (compared with C89) were deliberately not adopted in C++:
[1] Variable-length arrays (VLAs); use vector or some form of dynamic array
[2] Designated initializers; use constructors

The C11 grammar has the designated initializers [ISO/IEC 9899:2011 N1570 Committee Draft - April 12, 2011]

6.7.9 Initialization

    initializer:
        assignment-expression
        { initializer-list }
        { initializer-list , }
    initializer-list:
        designation_opt initializer
        initializer-list , designationopt initializer
    designation:
        designator-list =
    designator-list:
        designator
        designator-list designator
    designator:
        [ constant-expression ]
        . identifier

However, the C++11 grammar does not have designated initializers [ISO/IEC 14882:2011 N3690 Committee Draft - May 15, 2013]

8.5 Initializers

    initializer:
        brace-or-equal-initializer
        ( expression-list )
    brace-or-equal-initializer:
        = initializer-clause
        braced-init-list
    initializer-clause:
        assignment-expression
        braced-init-list
    initializer-list:
        initializer-clause ...opt
        initializer-list , initializer-clause ...opt
    braced-init-list:
        { initializer-list ,opt }
        { }
@guilhermeferreira
Copy link
Contributor Author

By the way, the error was caught by the AppVeyor during the unit testing compilation:

c:\projects\paho-mqtt-cpp\test\unit\token_test.h(146): error C2059: syntax error: '.'
c:\projects\paho-mqtt-cpp\test\unit\token_test.h(147): error C2143: syntax error: missing ';' before '}'
c:\projects\paho-mqtt-cpp\test\unit\token_test.h(149): error C2059: syntax error: 'switch'

@fpagliughi
Copy link
Contributor

The unit tests will be totally rewritten in #169.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants