C++ array Designator Initializer does not work with MSVC compiler

Mohammad Mohsin Siddiqui 175 Reputation points
2023-01-30T05:06:57.45+00:00

I have code like below which gets compiled successfully with LLVM clang-cl but fails with MSVC compiler. I believe MSVS supports designator initializers for array and struct initialization. Please let us know how we can use designator initialization for array with MSVC compiler

class A {

public:
    static constexpr int ZERO = 0;
    static constexpr int ONE = 1;
};


int main()
{
    int a[5]{ [A::ZERO] = 0,[A::ONE] = 1 };
    int b[2] = { [0] = 0,[1] = 1 };
    return 0;
}

Using MSVC compiler:

 

D:\Experimental>"C:\Program Files\\Microsoft Visual Studio\\2022\Professional\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64\cl.exe" /W4 /std:c++20  D:\Experimental\source.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.34.31937 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.
source.cpp
D:\Experimental\source.cpp(15): error C2760: syntax error: '::' was unexpected here; expected ','
D:\Experimental\source.cpp(15): error C2065: 'ZERO': undeclared identifier
D:\Experimental\source.cpp(15): error C2059: syntax error: ']'
D:\Experimental\source.cpp(15): error C2143: syntax error: missing ';' before '}'
D:\Experimental\source.cpp(16): error C2760: syntax error: 'constant' was unexpected here; expected 'expression'
D:\Experimental\source.cpp(16): error C2059: syntax error: ']'
D:\Experimental\source.cpp(16): error C2143: syntax error: missing ';' before '}'
D:\Experimental\source.cpp(16): error C2059: syntax error: '}'
D:\Experimental\source.cpp(17): error C2059: syntax error: 'return'
D:\Experimental\source.cpp(18): error C2059: syntax error: '}'
D:\Experimental\source.cpp(18): error C2143: syntax error: missing ';' before '}'

Using LLVM clang-cl

D:\Experimental>D:\TW\Tools\LLVM\bin\clang-cl.exe -std:c++20 -Wall -Wno-c99-designator -Wno-c++98-compat D:\Experimental\source.cpp
D:\Experimental\source.cpp(15,9): warning: unused variable 'a' [-Wunused-variable]
    int a[5] { [A::ZERO] = 0,[A::ONE] = 1 };
        ^
D:\Experimental\source.cpp(16,9): warning: unused variable 'b' [-Wunused-variable]
    int b[2] { [0] = 0,[1] = 1 };
        ^
2 warnings generated.

Developer technologies C++
{count} vote

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Viorel 122.5K Reputation points
    2023-01-30T06:46:38.86+00:00

    Designators seem to work for files that have the .c extension or are compiled with /TC option. The next test works:

    // compiled with /TC option
    
    struct S
    {
        int x, y;
    };
    
    void f( )
    {
        struct S s = { .y = 1, .x = 2 };
        struct S a[2] = { [0].x = 10, [1].y = 20 };
        int b[2] = { [1] = 0, [0] = 1 };
    }
    

    In Visual Studio it does not work for C++ code.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.