c++ - Why can I initialize a regular array from {}, but not a std::array -


this works:

int arr[10] = {}; 

all elements of arr value-initialized zero.

why doesn't work:

std::array<int, 10> arr({});  

i following warning g++ (version 4.8.2):

warning: missing initializer member ‘std::array<int, 10ul>::_m_elems’

there 2 issues 1 matter of style , warning.

although may not obvious, aggregate initialization happening on temporary being used argument copy constructor. more idiomatic initialization follows:

std::array<int, 10> arr = {};  

although still leaves warning.

the warning covered gcc bug report: - -wmissing-field-initializers relaxation request , 1 of comments says:

[...]surely, c++ syntax of saying mytype x = {}; should supported, seen here:

http://en.cppreference.com/w/cpp/language/aggregate_initialization

where instance:

struct s {   int a;   float b;   std::string str; };  s s = {}; // identical s s = {0, 0.0, std::string}; 

that shouldn't warn reasons stated in earlier comments.

and follow-up comment says:

my statement zero-initialization inaccurate (thanks), general point still stands: in c have write ' = {0}' since empty-braces initializer not supported language (you warning -pedantic); in c++, can write ' = {}' or 't foo = t();', don't need write ' = {0}' specifically.

the latest versions of gcc not produce warning case, see live working gcc 5.1.

we can see topic covered in clang developers lists in thead: -wmissing-field-initializers.

for reference draft c++11 standard section 8.5.1 [dcl.init.aggr] says:

if there fewer initializer-clauses in list there members in aggregate, each member not explicitly initialized shall initialized empty initializer list (8.5.4). [ example:

struct s { int a; const char* b; int c; }; s ss = { 1, "asdf" }; 

initializes ss.a 1, ss.b "asdf", , ss.c value of expression of form int(), is, 0. —end example ]

so valid c++, although noted using {} not valid c99. 1 argue warning, seems idiomatic c++ using {} aggregate initialization , problematic if using -werror turn warnings errors.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -