From c5e77313424a0503ff1c3e835c793f18d246e326 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Sat, 12 Mar 2022 15:06:47 -0500 Subject: [PATCH] wrong operator[] used in Index::indexof() ... to prevent such errors from compiling 2 ctors of Index are now explicit --- src/TiledArray/util/index.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/TiledArray/util/index.h b/src/TiledArray/util/index.h index e86560d970..53d8ccb1c1 100644 --- a/src/TiledArray/util/index.h +++ b/src/TiledArray/util/index.h @@ -22,7 +22,7 @@ std::string join(const small_vector &v); template using enable_if_string = std::enable_if_t< std::is_same_v, U>; -/// an n-index, with n a runtime parameter +/// an `n`-index, with `n` a runtime parameter template class Index { public: @@ -36,10 +36,10 @@ class Index { Index(const S &s) : data_(s.begin(), s.end()) {} template - Index(const std::string &s) : Index(index::tokenize(s)) {} + explicit Index(const std::string &s) : Index(index::tokenize(s)) {} template - Index(const char *s) : Index(std::string(s)) {} + explicit Index(const char *s) : Index(std::string(s)) {} template operator std::string() const { return index::join(data_); } @@ -65,11 +65,14 @@ class Index { const auto& operator[](size_t idx) const { return data_.at(idx); } + /// @param[in] v element to seek + /// @pre `this->contains(v)` + /// @return the location of @p v in `*this` size_t indexof(const T& v) const { for (size_t i = 0; i < this->size(); ++i) { - if (this[i] == v) return i; + if ((*this)[i] == v) return i; } - return -1; + TA_ASSERT(false); } /// Returns true if argument exists in the Index object, else returns false