diff --git base/containers/circular_deque.h base/containers/circular_deque.h index 0d452b56bedd..bf42a9584488 100644 --- base/containers/circular_deque.h +++ base/containers/circular_deque.h @@ -14,7 +14,6 @@ #include "base/containers/vector_buffer.h" #include "base/logging.h" #include "base/macros.h" -#include "base/stl_util.h" #include "base/template_util.h" // base::circular_deque is similar to std::deque. Unlike std::deque, the @@ -522,15 +521,15 @@ class circular_deque { return buffer_[i - right_size]; } value_type& at(size_type i) { - return const_cast(as_const(*this).at(i)); + return const_cast( + const_cast(this)->at(i)); } - value_type& operator[](size_type i) { - return const_cast(as_const(*this)[i]); + value_type& operator[](size_type i) { return at(i); } + const value_type& operator[](size_type i) const { + return const_cast(this)->at(i); } - const value_type& operator[](size_type i) const { return at(i); } - value_type& front() { DCHECK(!empty()); return buffer_[begin_]; diff --git base/containers/flat_tree.h base/containers/flat_tree.h index b7549dd5b0c6..89c75d70692e 100644 --- base/containers/flat_tree.h +++ base/containers/flat_tree.h @@ -10,7 +10,6 @@ #include #include -#include "base/stl_util.h" #include "base/template_util.h" namespace base { @@ -341,6 +340,8 @@ class flat_tree { const key_compare& key_comp_; }; + const flat_tree& as_const() { return *this; } + iterator const_cast_it(const_iterator c_it) { auto distance = std::distance(cbegin(), c_it); return std::next(begin(), distance); @@ -776,7 +777,7 @@ template template auto flat_tree::find(const K& key) -> iterator { - return const_cast_it(as_const(*this).find(key)); + return const_cast_it(as_const().find(key)); } template @@ -799,7 +800,7 @@ template template auto flat_tree::equal_range( const K& key) -> std::pair { - auto res = as_const(*this).equal_range(key); + auto res = as_const().equal_range(key); return {const_cast_it(res.first), const_cast_it(res.second)}; } @@ -820,7 +821,7 @@ template template auto flat_tree::lower_bound( const K& key) -> iterator { - return const_cast_it(as_const(*this).lower_bound(key)); + return const_cast_it(as_const().lower_bound(key)); } template @@ -841,7 +842,7 @@ template template auto flat_tree::upper_bound( const K& key) -> iterator { - return const_cast_it(as_const(*this).upper_bound(key)); + return const_cast_it(as_const().upper_bound(key)); } template diff --git base/stl_util.h base/stl_util.h index 83d86ad90d24..4edd1fb9e20b 100644 --- base/stl_util.h +++ base/stl_util.h @@ -158,16 +158,6 @@ constexpr const T* data(const std::array& array) noexcept { return !array.empty() ? &array[0] : nullptr; } -// C++14 implementation of C++17's std::as_const(): -// https://en.cppreference.com/w/cpp/utility/as_const -template -constexpr std::add_const_t& as_const(T& t) noexcept { - return t; -} - -template -void as_const(const T&& t) = delete; - // Returns a const reference to the underlying container of a container adapter. // Works for std::priority_queue, std::queue, and std::stack. template diff --git base/values.cc base/values.cc index fbc859c6518d..95e8fc815067 100644 --- base/values.cc +++ base/values.cc @@ -24,20 +24,6 @@ namespace base { -// base::Value must be standard layout to guarantee that writing to -// |bool_type_| then reading |type_| is defined behaviour. See: -// -// [class.union]: -// If a standard-layout union contains several standard-layout structs that -// share a common initial sequence (9.2), and if an object of this -// standard-layout union type contains one of the standard-layout structs, -// it is permitted to inspect the common initial sequence of any of -// standard-layout struct members; -// -static_assert(std::is_standard_layout::value, - "base::Value should be a standard-layout C++ class in order " - "to avoid undefined behaviour in its implementation!"); - static_assert(sizeof(Value::DoubleStorage) == sizeof(double), "The double and DoubleStorage types should have the same size"); @@ -433,7 +419,7 @@ void Value::ClearList() { } Value* Value::FindKey(StringPiece key) { - return const_cast(as_const(*this).FindKey(key)); + return const_cast(static_cast(this)->FindKey(key)); } const Value* Value::FindKey(StringPiece key) const { @@ -445,7 +431,8 @@ const Value* Value::FindKey(StringPiece key) const { } Value* Value::FindKeyOfType(StringPiece key, Type type) { - return const_cast(as_const(*this).FindKeyOfType(key, type)); + return const_cast( + static_cast(this)->FindKeyOfType(key, type)); } const Value* Value::FindKeyOfType(StringPiece key, Type type) const { @@ -569,7 +556,7 @@ Optional Value::ExtractKey(StringPiece key) { } Value* Value::FindPath(StringPiece path) { - return const_cast(as_const(*this).FindPath(path)); + return const_cast(const_cast(this)->FindPath(path)); } const Value* Value::FindPath(StringPiece path) const { @@ -584,7 +571,8 @@ const Value* Value::FindPath(StringPiece path) const { } Value* Value::FindPathOfType(StringPiece path, Type type) { - return const_cast(as_const(*this).FindPathOfType(path, type)); + return const_cast( + const_cast(this)->FindPathOfType(path, type)); } const Value* Value::FindPathOfType(StringPiece path, Type type) const { @@ -627,7 +615,8 @@ const std::string* Value::FindStringPath(StringPiece path) const { } std::string* Value::FindStringPath(StringPiece path) { - return const_cast(as_const(*this).FindStringPath(path)); + return const_cast( + static_cast(this)->FindStringPath(path)); } const Value::BlobStorage* Value::FindBlobPath(StringPiece path) const { @@ -713,11 +702,11 @@ Optional Value::ExtractPath(StringPiece path) { // DEPRECATED METHODS Value* Value::FindPath(std::initializer_list path) { - return const_cast(as_const(*this).FindPath(path)); + return const_cast(const_cast(this)->FindPath(path)); } Value* Value::FindPath(span path) { - return const_cast(as_const(*this).FindPath(path)); + return const_cast(const_cast(this)->FindPath(path)); } const Value* Value::FindPath(std::initializer_list path) const { @@ -736,11 +725,13 @@ const Value* Value::FindPath(span path) const { Value* Value::FindPathOfType(std::initializer_list path, Type type) { - return const_cast(as_const(*this).FindPathOfType(path, type)); + return const_cast( + const_cast(this)->FindPathOfType(path, type)); } Value* Value::FindPathOfType(span path, Type type) { - return const_cast(as_const(*this).FindPathOfType(path, type)); + return const_cast( + const_cast(this)->FindPathOfType(path, type)); } const Value* Value::FindPathOfType(std::initializer_list path, @@ -902,7 +893,7 @@ bool Value::GetAsString(string16* out_value) const { bool Value::GetAsString(const Value** out_value) const { if (out_value && is_string()) { - *out_value = this; + *out_value = static_cast(this); return true; } return is_string(); @@ -1300,7 +1291,9 @@ bool DictionaryValue::Get(StringPiece path, } bool DictionaryValue::Get(StringPiece path, Value** out_value) { - return as_const(*this).Get(path, const_cast(out_value)); + return static_cast(*this).Get( + path, + const_cast(out_value)); } bool DictionaryValue::GetBoolean(StringPiece path, bool* bool_value) const { @@ -1373,7 +1366,8 @@ bool DictionaryValue::GetBinary(StringPiece path, } bool DictionaryValue::GetBinary(StringPiece path, Value** out_value) { - return as_const(*this).GetBinary(path, const_cast(out_value)); + return static_cast(*this).GetBinary( + path, const_cast(out_value)); } bool DictionaryValue::GetDictionary(StringPiece path, @@ -1391,8 +1385,9 @@ bool DictionaryValue::GetDictionary(StringPiece path, bool DictionaryValue::GetDictionary(StringPiece path, DictionaryValue** out_value) { - return as_const(*this).GetDictionary( - path, const_cast(out_value)); + return static_cast(*this).GetDictionary( + path, + const_cast(out_value)); } bool DictionaryValue::GetList(StringPiece path, @@ -1409,8 +1404,9 @@ bool DictionaryValue::GetList(StringPiece path, } bool DictionaryValue::GetList(StringPiece path, ListValue** out_value) { - return as_const(*this).GetList(path, - const_cast(out_value)); + return static_cast(*this).GetList( + path, + const_cast(out_value)); } bool DictionaryValue::GetWithoutPathExpansion(StringPiece key, @@ -1427,8 +1423,9 @@ bool DictionaryValue::GetWithoutPathExpansion(StringPiece key, bool DictionaryValue::GetWithoutPathExpansion(StringPiece key, Value** out_value) { - return as_const(*this).GetWithoutPathExpansion( - key, const_cast(out_value)); + return static_cast(*this).GetWithoutPathExpansion( + key, + const_cast(out_value)); } bool DictionaryValue::GetBooleanWithoutPathExpansion(StringPiece key, @@ -1494,8 +1491,11 @@ bool DictionaryValue::GetDictionaryWithoutPathExpansion( bool DictionaryValue::GetDictionaryWithoutPathExpansion( StringPiece key, DictionaryValue** out_value) { - return as_const(*this).GetDictionaryWithoutPathExpansion( - key, const_cast(out_value)); + const DictionaryValue& const_this = + static_cast(*this); + return const_this.GetDictionaryWithoutPathExpansion( + key, + const_cast(out_value)); } bool DictionaryValue::GetListWithoutPathExpansion( @@ -1514,8 +1514,10 @@ bool DictionaryValue::GetListWithoutPathExpansion( bool DictionaryValue::GetListWithoutPathExpansion(StringPiece key, ListValue** out_value) { - return as_const(*this).GetListWithoutPathExpansion( - key, const_cast(out_value)); + return + static_cast(*this).GetListWithoutPathExpansion( + key, + const_cast(out_value)); } bool DictionaryValue::Remove(StringPiece path, @@ -1645,7 +1647,9 @@ bool ListValue::Get(size_t index, const Value** out_value) const { } bool ListValue::Get(size_t index, Value** out_value) { - return as_const(*this).Get(index, const_cast(out_value)); + return static_cast(*this).Get( + index, + const_cast(out_value)); } bool ListValue::GetBoolean(size_t index, bool* bool_value) const { @@ -1702,8 +1706,9 @@ bool ListValue::GetDictionary(size_t index, } bool ListValue::GetDictionary(size_t index, DictionaryValue** out_value) { - return as_const(*this).GetDictionary( - index, const_cast(out_value)); + return static_cast(*this).GetDictionary( + index, + const_cast(out_value)); } bool ListValue::GetList(size_t index, const ListValue** out_value) const { @@ -1719,8 +1724,9 @@ bool ListValue::GetList(size_t index, const ListValue** out_value) const { } bool ListValue::GetList(size_t index, ListValue** out_value) { - return as_const(*this).GetList(index, - const_cast(out_value)); + return static_cast(*this).GetList( + index, + const_cast(out_value)); } bool ListValue::Remove(size_t index, std::unique_ptr* out_value) {