aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Moore <sethmo@google.com>2021-07-09 05:50:50 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-07-09 05:50:50 +0000
commit715b0a323388434d8f6b3d95cdeaecc5a3af5ad7 (patch)
treea7b0c82cc4b342d22a22e671d64980a5384e3949
parentd4ef5930e6a75d31ba586b70762d31f7baf4c142 (diff)
parenta3bc7d7b212013e20391ada0c1eb4c164e2fba3f (diff)
downloadlibcppbor-715b0a323388434d8f6b3d95cdeaecc5a3af5ad7.tar.gz
Don't allow implicit conversion to Array am: b0c9795e04 am: 1b23995a6b am: d7ace4469d am: a3bc7d7b21
Original change: https://android-review.googlesource.com/c/platform/external/libcppbor/+/1753047 Change-Id: Id76ace8e4758a195064a284c0061d1a30105eb60
-rw-r--r--include/cppbor/cppbor.h20
1 files changed, 17 insertions, 3 deletions
diff --git a/include/cppbor/cppbor.h b/include/cppbor/cppbor.h
index b78c41f..9bad233 100644
--- a/include/cppbor/cppbor.h
+++ b/include/cppbor/cppbor.h
@@ -598,6 +598,13 @@ class Array : public Item {
Array(Args&&... args);
/**
+ * The above variadic constructor is disabled if sizeof(Args) != 1, so special
+ * case an explicit Array constructor for creating an Array with one Item.
+ */
+ template <typename T, typename Enable>
+ explicit Array(T&& v);
+
+ /**
* Append a single element to the Array, of any compatible type.
*/
template <typename T>
@@ -1039,14 +1046,21 @@ inline void map_helper(Map& map, Key&& key, Value&& value, Rest&&... rest) {
} // namespace details
template <typename... Args,
- /* Prevent use as copy ctor */ typename = std::enable_if_t<
- (sizeof...(Args)) != 1 ||
- !(std::is_same_v<Array, std::remove_cv_t<std::remove_reference_t<Args>>> || ...)>>
+ /* Prevent implicit construction with a single argument. */
+ typename = std::enable_if_t<(sizeof...(Args)) != 1>>
Array::Array(Args&&... args) {
mEntries.reserve(sizeof...(args));
(mEntries.push_back(details::makeItem(std::forward<Args>(args))), ...);
}
+template <typename T,
+ /* Prevent use as copy constructor. */
+ typename = std::enable_if_t<
+ !std::is_same_v<Array, std::remove_cv_t<std::remove_reference_t<T>>>>>
+Array::Array(T&& v) {
+ mEntries.push_back(details::makeItem(std::forward<T>(v)));
+}
+
template <typename T>
Array& Array::add(T&& v) & {
mEntries.push_back(details::makeItem(std::forward<T>(v)));