3

Here is my code:

#include <array>

struct A
{
    A(int) {}
};


std::array<A, 42> a;


int main()
{
    return 0;
}

And the error:

$ g++ -Wall -Wextra -Wpedantic main.cpp 
main.cpp:9:19: error: use of deleted function `std::array<A, 42ul>::array()'
 std::array<A, 42> a;
                   ^
In file included from main.cpp:1:0:
/usr/include/c++/6/array:90:12: note: `std::array<A, 42ul>::array()' is implicitly deleted because the default definition would be ill-formed:
     struct array
            ^~~~~
/usr/include/c++/6/array:90:12: error: no matching function for call to `A::A()'
main.cpp:5:5: note: candidate: A::A(int)
     A(int) {}
     ^
main.cpp:5:5: note:   candidate expects 1 argument, 0 provided
main.cpp:3:8: note: candidate: constexpr A::A(const A&)
 struct A
        ^
main.cpp:3:8: note:   candidate expects 1 argument, 0 provided
main.cpp:3:8: note: candidate: constexpr A::A(A&&)
main.cpp:3:8: note:   candidate expects 1 argument, 0 provided

My environment:

$ g++ --version
g++ (Debian 6.3.0-6) 6.3.0 20170205

Related:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53248

Why I expected this to work:

std::array does not initialize its elements upon creation. The size of A is well defined.

Vorac
  • 8,726
  • 11
  • 58
  • 101
  • 2
    Hmmm... What makes you think they're not initialized? [Because I think they are](http://coliru.stacked-crooked.com/a/f5ee64cd274919ec) – Borgleader Mar 29 '17 at 13:58
  • 2
    `std::array` does indeed initialize the elements in the array it wraps, or rather it *constructs* all elements. Just like an old C-style array. What makes you think the element construction doesn't happen? – Some programmer dude Mar 29 '17 at 13:58
  • 2
    Saying `A arr[10];` is enough to invoke the default constructor of `A` 10 times. That's what's happening here. `std::array` merely wraps over a C array. – DeiDei Mar 29 '17 at 13:59
  • @Borgleader because I tried with std::array and they were not zeroed out. Oh, well, it seems `int i()` ~= `int i{}`. – Vorac Mar 29 '17 at 14:05
  • 1
    The main case of no initialization in C++ is that default-initialization for primitive types like `int` performs no initialization. All of this higher level stuff doesn't have an easy choice to just not initialize something - it all uses at least default-initialization. When something isn't initialized at all, it's rather explicit, like `std::optional`. – chris Mar 29 '17 at 14:07

0 Answers0