Let's consider this class:
class A {
public:
A() = delete;
A(int i) :
i_m(i) {
std::cout << __PRETTY_FUNCTION__ << ' ' << i_m << '\n';
}
~A() {
std::cout << __PRETTY_FUNCTION__ << ' ' << i_m << '\n';
}
private:
int i_m{1234567890};
};
The default constructor is explicitly deleted so AFAIK A
can only be constructed from an integer. The default initialization of data member i_m
will never be used.
Let'ts consider this program:
int main() {
using T = std::array<A, 2>;
//T a;
// error: use of deleted function 'std::array<A, 2>::array()'
// note: 'std::array<A, 2>::array()' is implicitly deleted because the default definition would be ill-formed
// error: use of deleted function 'A::A()'
//T b{};
// error: use of deleted function 'A::A()'
}
Again, this seems completely fine to me.
Let's now consider this other program:
int main() {
using T = std::array<A, 2>;
auto foo = new T{};
delete foo;
auto foo_init = new T{1, 2};
delete foo_init;
// auto zorg = new T();
// delete zorg;
// error: use of deleted function 'std::array<A, 2>::array()'
// note: 'std::array<A, 2>::array()' is implicitly deleted because the default definition would be ill-formed:
// error: use of deleted function 'A::A()'
auto zorg_init = new T({3, 4});
delete zorg_init;
}
This code does compile (with no warning) and generates the following output:
A::~A() 0
A::~A() 38870160
A::A(int) 1
A::A(int) 2
A::~A() 2
A::~A() 1
A::A(int) 3
A::A(int) 4
A::~A() 4
A::~A() 3
And now something seems not fine to me. How it that possible that the line auto foo = new T{};
is not considered ill-formed? The initialization of class A
is completely bypassed here.
It is worth nothing that this code doesn't compile:
int main() {
auto p = new A{};
delete p;
}
The error being the expected:
error: use of deleted function 'A::A()'
I tested those codes with the following options: -Wall -Wextra -pedantic -std=c++17
. gcc -v
gives: gcc version 7.2.0 (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project)
on my computer.
Any explanation will be greatly appreciated :)
PS : after exhaustive tests in Compiler Explorer (using with sample https://godbolt.org/z/5VZLU_), it seems that this is an issue in gcc 7.x. Indeed, only 7.x versions of gcc compile this sample. gcc 6.4 doesn't not. gcc 8.1 neither. clang neither. msvc neither.
Can you confirm that there is no UB here and this is really a compiler bug?