인벤토리 기능을 구현하고 있는데 스마트포인터를 내가 제대로 이해한건지 잘 모르겠음
인벤토리는 현재 vector<vector<shared_ptr<Item>>> _items; 이차원 벡터로 가지고 있음
template<typename T>
bool Inventory::AddItem(shared_ptr<T> item)
{
static_assert(is_convertible_v<shared_ptr<T>, shared_ptr<Item>>, "T must be derived from Item");
shared_ptr<Item> emptySlot = nullptr;
const int itemIdx = item->GetItemTypeIndex();
for (auto& slot : _items[itemIdx])
{
if (slot == item)
{
slot->SetItemCount(slot->GetItemCount() + 1);
return true;
}
if (!slot && !emptySlot)
emptySlot = &slot;
}
if (emptySlot)
{
emptySlot = item;
return true;
}
return false;
}
인벤토리로 아이템을 추가할 때,
1. 이미 아이템을 가지고 있는가?
2. 빈 슬롯이 존재하는가?
위 2가지를 검사하고 추가하는 방향으로 잡았는데
auto& 이 부분을 참조로 하는게 맞는 것인지 잘 모르겠음...
auto는 레퍼런스가 되어주지 않기 때문에 auto&를 하지 않으면 벡터가 복사되어서 성능 개박살남
예전에 메이플이었나 특정 기능에서 수상할 정도로 속도가 느려서 트러블 슈팅 해보니까 auto&를 해야할 걸 auto로 하고있었다는 글이 있음
넥슨 기본기 박살났노
굳이 shared를 해야하나
이게 컴파일이 됨?
만지다보니 템플릿 기능이 불필요해서 해결했네요
auto는 auto가 어떤 타입으로 추론될지 정확히 알고있을때 써라
아니면 에디터 기능중에 Inlay Hint라는 기능있는데 그거 써봐도 되고