diff --git a/include/knowhere/dataset.h b/include/knowhere/dataset.h index 8e443e649..a223f0b03 100644 --- a/include/knowhere/dataset.h +++ b/include/knowhere/dataset.h @@ -35,34 +35,21 @@ class DataSet : public std::enable_shared_from_this { return; } for (auto&& x : this->data_) { - { - auto ptr = std::get_if<0>(&x.second); - if (ptr != nullptr) { - delete[] * ptr; - } - } - { - auto ptr = std::get_if<1>(&x.second); - if (ptr != nullptr) { - delete[] * ptr; - } - } - { - auto ptr = std::get_if<2>(&x.second); - if (ptr != nullptr) { - delete[] * ptr; - } - } - { - auto ptr = std::get_if<3>(&x.second); - if (ptr != nullptr) { - if (is_sparse) { - delete[](sparse::SparseRow*)(*ptr); - } else { - delete[](char*)(*ptr); + std::visit( + [](auto&& arg) { + using T = std::decay_t; + if constexpr (std::is_same_v || std::is_same_v || + std::is_same_v) { + if (arg != nullptr) { + delete[] arg; + } + } else if constexpr (std::is_same_v) { + if (arg != nullptr) { + delete[](char*)(arg); + } } - } - } + }, + x.second); } }