Ben GCC 7 bulundu garantili kopya elision uygulamıştır ve ben wandbox aşağıdaki kodu çalıştı:Garantili kopya elision ve Nonmoveable {Nonmoveable {}}
#include <iostream>
struct NonMovable
{
NonMovable() noexcept = default;
NonMovable(NonMovable&&) noexcept = delete;
NonMovable& operator=(NonMovable&&) noexcept = delete;
};
NonMovable Make()
{
return {};
}
int main()
{
//[[maybe_unused]] const auto x = Make();
//const auto z = NonMovable{};
[[maybe_unused]] const auto y = NonMovable{NonMovable{}};
}
Ve hatayı derlemek var:
prog.cc: In function 'int main()':
prog.cc:20:60: error: use of deleted function 'NonMovable::NonMovable(NonMovable&&)'
[[maybe_unused]] const auto y = NonMovable{NonMovable{}};
^
prog.cc:6:5: note: declared here
NonMovable(NonMovable&&) noexcept = delete;
^~~~~~~~~~
cppreference göre:
In initialization, if the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object:
T x = T(T(T())); // only one call to default constructor of T, to initialize x
yüzden ambalajlama olması gerektiğini düşünüyorum al, const Movable y{};
. Sorun nedir?
Görünüşe göre bir GCC hatası. Kod, yeni bir GCC yapısı üzerinde iyi bir derleme yapıyor. – Cu2S