Mam małą funkcję lambda, która odnajdzie i zwróci QTreeWidgetItem
. Ale jeśli nie znajdzie danego przedmiotu, zwróci on nullptr
. Ale jeśli spróbuję to skompilować, to powoduje to błąd.Jak zwrócić nullptr z funkcji lambda?
Funkcja:
auto takeTopLevelItem = [](QTreeWidget* aTreeWidget, const QString& aText)
{
const int count = aTreeWidget->topLevelItemCount();
for (int index = 0; index < count; ++index)
{
auto item = aTreeWidget->topLevelItem(index);
if (item->text(0) == aText)
{
return aTreeWidget->takeTopLevelItem(index);
}
}
return nullptr; // This causes a compilation error.
};
Błąd:
Error 1 error C3487: 'nullptr': all return expressions in a lambda must have the same type: previously it was 'QTreeWidgetItem *' cpp 251
Zmieniłem wspomniany wiersz z tego i teraz kompiluje:
return (QTreeWidgetItem*)(nullptr);
ale chciałbym tego uniknąć składnia. Jak mogę to rozwiązać?
Używam Visual Studio 2012
.
To wygląda jak błąd w VS2012? – Yakk
@Yakk: Dlaczego? Komunikat o błędzie jest całkiem jasny, prawda? – MSalters
@MSalters Hmm. Dziwne, tak naprawdę uważałem, że tak długo, jak późniejsze typy powrotu są kompatybilne (domyślnie wymienialne) z pierwszym typem zwracania, wszystko było w porządku. – Yakk