Gabe Black has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/45901 )

Change subject: base: Apply the stl_helpers helper judiciously.
......................................................................

base: Apply the stl_helpers helper judiciously.

The existing template would apply the helper operator to *any* template
which took two types, regardless of what that template was. The
assumption was that those types *must* be STL containers, because no
other template takes two types, right?

Instead, this new version uses type traits to explicitly whitelist types
which the helper applies to. Currently the only type it seems to be used
with is std::vector, but by defining more specializations of
IsHelpedContainer, other types/templates can be enabled as well.

This is particularly important when moving to c++17, since the
std::string class would then apparently match the old overload. That makes
the << operator ambiguous and breaks the build.

Change-Id: Id283746a2ccced8882fa23e6f9e69fe22e206b70
---
M src/base/stl_helpers.hh
1 file changed, 14 insertions(+), 5 deletions(-)



diff --git a/src/base/stl_helpers.hh b/src/base/stl_helpers.hh
index 51dc039..2d7cb64 100644
--- a/src/base/stl_helpers.hh
+++ b/src/base/stl_helpers.hh
@@ -31,6 +31,8 @@

 #include <algorithm>
 #include <iostream>
+#include <type_traits>
+#include <vector>

 namespace m5 {
 namespace stl_helpers {
@@ -41,19 +43,26 @@
  *
  * @ingroup api_base_utils
  */
-template <template <typename T, typename A> class C, typename T, typename A>
-std::ostream &
-operator<<(std::ostream& out, const C<T,A> &vec)
+
+template <typename T, typename Enabled=void>
+struct IsHelpedContainer : public std::false_type {};
+
+template <typename ...Types>
+struct IsHelpedContainer<std::vector<Types...>> : public std::true_type {};
+
+template <typename T>
+std::enable_if_t<IsHelpedContainer<T>::value, std::ostream &>
+operator<<(std::ostream& out, const T &t)
 {
     out << "[ ";
     bool first = true;
-    auto printer = [&first, &out](const T &elem) {
+    auto printer = [&first, &out](const auto &elem) {
         if (first)
             out << elem;
         else
             out << " " << elem;
     };
-    std::for_each(vec.begin(), vec.end(), printer);
+    std::for_each(t.begin(), t.end(), printer);
     out << " ]";
     out << std::flush;
     return out;

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/45901
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Id283746a2ccced8882fa23e6f9e69fe22e206b70
Gerrit-Change-Number: 45901
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <gabe.bl...@gmail.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to