When I compile the following piece of code with CLANG:
#include <iostream>
#include <array>
#include <algorithm>
#include <functional>
int main() {
std::array<int, 2> a = {1, 2};
std::array<int, 2> b = {2, 1};
std::array<int, 2> c;
std::transform(a.begin(), a.end(), b.begin(), c.begin(), std::multiplies<int>());
for(auto &&i : c) std::cout << i << " ";
std::cout << std::endl;
}
by issuing the command:
clang++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp
It issues the warning:
warning: suggest braces around initialization of subobject [-Wmissing-braces]
However, GCC compiles this program with out issuing a warning at all.
Q:
- Which compiler is right?
- What's the reason that Clangs warns me?
In some cases, braces can be elided. This is one of those cases. The outer-most braces for initializing
a
andb
are optional. It is syntactically correct either way - but it's clearer to just include them. Clang is just warning you (warning, not error) about this - it's a perfectly valid warning. And as chris, points out, with-Wmissing-braces
, gcc issues the same warning. Ultimately, both compilers accept the code, which is correct; it is, after all, a valid program. That's all that matters.From [dcl.init.aggr]: