I am using GCC 5.4 for compiling a test program in C 14.
#include <type_traits>
#include <list>
#include <iostream>
#include <memory>
int main()
{
int VALUE = 42;
const auto list_ = {
std::make_unique<int>(VALUE),
std::make_unique<int>(0),
std::make_unique<int>(0)
};
}
GCC 5.4 fails with the below error message:
<source>: In function 'int main()':
<source>:13:5: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'
};
^
In file included from /opt/compiler-explorer/gcc-5.4.0/include/c /5.4.0/memory:81:0,
from <source>:4:
/opt/compiler-explorer/gcc-5.4.0/include/c /5.4.0/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^
The same code compiles properly with Clang 3.5. See https://godbolt.org/z/PM776xGP4
The issue seems to be there until GCC 9.2 where it compiles properly.
Is this a known bug in GCC 9.1 and below? If yes, is there a way to solve this using the initializer list?
CodePudding user response:
You can use:
const std::initializer_list<std::unique_ptr<int>> list{
std::make_unique< int >( 42 ),
std::make_unique< int >( 0 ),
std::make_unique< int >( 0 )
};
Demo (old gcc-5.4 tested).