Код: Выделить всё
struct FooBar {
int a;
int b;
FooBar& operator = (const FooBar& other) = delete;
FooBar(const FooBar& other) = delete;
FooBar(FooBar&& other) noexcept = default;
FooBar& operator = (FooBar&& other) = default;
FooBar(int a, int b)
: a(a)
, b(b)
{}
};
Код: Выделить всё
std::mapКод: Выделить всё
auto container = std::map{
{"someKey", FooBar{1, 2}}
};
нет соответствующей функции для вызова 'construct_at(std::pair*&, const std::pair&)'
It похоже, копирует временный объект std::pair вместо его перемещения, что приводит к ошибке при попытке скопировать временный объект FooBar:
использование удаленной функции 'constexpr std::pair::pair(const std::pair&) [with _T1 = const std::__cxx11:: базовая_строка; _T2 = FooBar]'
96 | -> decltype(::new((void*)0) _Tp(std::declval()...))
В файле, включенном в /opt/compiler-explorer/gcc-13.2.0 /include/c++/13.2.0/bits/stl_algobase.h:64:
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/stl_pair.h:197: 17: примечание: 'constexpr std::pair::pair(const std::pair&) [with _T1 = const std::__cxx11::basic_string; _T2 = FooBar]' неявно удаляется, поскольку определение по умолчанию будет неверным:
197 | constexpr пара(const пара&) = по умолчанию; ///< Конструктор копирования
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/stl_pair.h:197:17: ошибка: использование удаленной функции 'FooBar: :FooBar(const FooBar&)' :11:5: примечание: объявлено здесь
11 | FooBar(const FooBar& другие) = удалить;
Я могу использовать std:
Код: Выделить всё
auto container = std::map{};
container.emplace("someKey", FooBar{1, 2});
Можно ли как-нибудь переместить временный объект в контейнер, а не копировать его в момент создания контейнера?
Подробнее здесь: https://stackoverflow.com/questions/786 ... oved-not-c