Compound Literals are a C99 construct. Even though I can do this in C++ :
#include <iostream>
using namespace std;
int main() {
for (auto i : (float[2]) {2.7, 3.1}) cout << i << endl;
}
It seems that for example MSVC supports it as an extension. Yet all compilers I can get my hands on, compile the above mentioned code.
So is this a feature available in C++14 ? Is there a different standard term (It looks to me like just creating a temporary using braced initialization) ?
Side Note : "Compound Literals" (or whatever I should call the above) are a pack expansion context ( just to mention a functionality )
This is an extension that both gcc and
clang
support. The gcc document says:if you build with -pedantic you should receive a warning, for example
clang
says (see it live):Note, the semantic differences in C++ are not minor and code that would be well-defined in C99 can have undefined behavior in C++ with this extension:
is a C99 compound literal. Some compilers support it in C++ as an extension.
is a syntax error.
Given
using arr = float[2];
,is valid C++ that list-initializes a temporary array of two
float
s.is called a braced-init-list.
Finally, for your code,
works equally well and is perfectly valid C++ - this constructs a
std::initializer_list<double>
under the hood. If you really wantfloat
s, add thef
suffix to the numbers.