aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport/array-view.h
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2025-02-09 00:51:00 -0500
committerSimon Marchi <simon.marchi@efficios.com>2025-02-10 11:17:23 -0500
commit9044044c27c3619a18f58c8c235250264cd95a7e (patch)
tree31047d51cd7b89de6b86688ca3e6e83a926eab3c /gdbsupport/array-view.h
parenta4242dc3f5fa34a1686237c8b40a29d5fad1bcea (diff)
downloadbinutils-9044044c27c3619a18f58c8c235250264cd95a7e.zip
binutils-9044044c27c3619a18f58c8c235250264cd95a7e.tar.gz
binutils-9044044c27c3619a18f58c8c235250264cd95a7e.tar.bz2
gdbsupport: add gdb::make_array_view overload to create from an array
I think this overload will be useful for the following reasons. Consider a templated function like this: template <typename T> void func(gdb::array_view<T> view) {} Trying to pass an array to this function doesn't work, as template argument deduction fails: test.c:698:8: error: no matching function for call to ‘func(int [12])’ 698 | func (array); | ~~~~~^~~~~~~ test.c:686:6: note: candidate: ‘template<class T> void func(gdb::array_view<U>)’ 686 | void func(gdb::array_view<T> view) {} | ^~~~ test.c:686:6: note: template argument deduction/substitution failed: test.c:698:8: note: mismatched types ‘gdb::array_view<U>’ and ‘int*’ 698 | func (array); | ~~~~~^~~~~~~ Similarly, trying to compare a view with an array doesn't work. This: int array[12]; gdb::array_view<int> view; if (view == array) {} ... fails with: test.c:698:8: error: no matching function for call to ‘func(int [12])’ 698 | func (array); | ~~~~~^~~~~~~ test.c:686:6: note: candidate: ‘template<class T> void func(gdb::array_view<U>)’ 686 | void func(gdb::array_view<T> view) {} | ^~~~ test.c:686:6: note: template argument deduction/substitution failed: test.c:698:8: note: mismatched types ‘gdb::array_view<U>’ and ‘int*’ 698 | func (array); | ~~~~~^~~~~~~ With this new overload, we can do: func (gdb::make_array_view (array)); and if (view == gdb::make_array_view (array)) {} This is not ideal, I wish that omitting `gdb::make_array_view` would just work, but at least it allows creating an array view and have the element type automatically deduced from the array type. If someone knows how to make these cases "just work", I would be happy to know how. Change-Id: I6a71919d2d5a385e6826801d53f5071b470fef5f Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdbsupport/array-view.h')
-rw-r--r--gdbsupport/array-view.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index 5054d70..5dcf8d8 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -294,6 +294,15 @@ make_array_view (U *array, size_t size) noexcept
return {array, size};
}
+/* Create an array view from an array. */
+
+template <typename U, std::size_t Size>
+constexpr inline array_view<U>
+make_array_view (U (&array)[Size])
+{
+ return {array};
+}
+
} /* namespace gdb */
#endif /* GDBSUPPORT_ARRAY_VIEW_H */