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.