[libsigcplusplus] tuple_for_each(): Use of constexpr.



commit b2d67da5d135011e31e31b9ef463c43d9a49a496
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Mar 8 14:23:03 2016 +0100

    tuple_for_each(): Use of constexpr.

 sigc++/tuple-utils/tuple_for_each.h |   13 ++++++++++---
 tests/test_tuple_for_each.cc        |   19 +++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)
---
diff --git a/sigc++/tuple-utils/tuple_for_each.h b/sigc++/tuple-utils/tuple_for_each.h
index 760b52d..43dab3c 100644
--- a/sigc++/tuple-utils/tuple_for_each.h
+++ b/sigc++/tuple-utils/tuple_for_each.h
@@ -29,7 +29,9 @@ template <template <typename> class T_visitor, std::size_t size_from_index,
   typename... T_extras>
 struct tuple_for_each_impl {
   template <typename T>
-  static void
+  constexpr
+  static
+  void
   tuple_for_each(T&& t, T_extras&&... extras) {
     //We use std::decay_t<> because tuple_size is not defined for references.
     constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
@@ -49,7 +51,9 @@ struct tuple_for_each_impl {
 template <template <typename> class T_visitor, typename... T_extras>
 struct tuple_for_each_impl<T_visitor, 1, T_extras...> {
   template <typename T>
-  static void
+  constexpr
+  static
+  void
   tuple_for_each(T&& t, T_extras&&... extras) {
     //We use std::decay_t<> because tuple_size is not defined for references.
     constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
@@ -66,7 +70,9 @@ struct tuple_for_each_impl<T_visitor, 1, T_extras...> {
 template <template <typename> class T_visitor, typename... T_extras>
 struct tuple_for_each_impl<T_visitor, 0, T_extras...> {
   template <typename T>
-  static void
+  constexpr
+  static
+  void
   tuple_for_each(T&& /* t */, T_extras&&... /* extras */) {
     //Do nothing because the tuple has no elements.
   }
@@ -87,6 +93,7 @@ struct tuple_for_each_impl<T_visitor, 0, T_extras...> {
  * @param extras Any extra arguments to pass to @e T_Visitor's visit() method.
  */
 template <template <typename> class T_visitor, typename T, typename... T_extras>
+constexpr
 void
 tuple_for_each(T&& t, T_extras&&... extras) {
   //We use std::decay_t<> because tuple_size is not defined for references.
diff --git a/tests/test_tuple_for_each.cc b/tests/test_tuple_for_each.cc
index 2c6ab3c..ec5f42e 100644
--- a/tests/test_tuple_for_each.cc
+++ b/tests/test_tuple_for_each.cc
@@ -123,6 +123,17 @@ public:
   }
 };
 
+// A const char* will be converted to an int:
+template <>
+class visitor_with_specializations<const char*> {
+public:
+  static void
+  visit(const char* from) {
+    std::cout << "visitor_with_specializations::visit(): " << std::stoi(from)
+              << std::endl;
+  }
+};
+
 void
 test_tuple_for_each_multiple_types() {
   auto t_original = std::make_tuple(1, (double)2.1f, std::string("3"));
@@ -198,6 +209,12 @@ test_tuple_for_each_empty_tuple() {
   sigc::internal::tuple_for_each<for_each_simple>(t);
 }
 
+constexpr
+void
+test_tuple_for_each_constexpr() {
+  constexpr auto t_original = std::make_tuple(1, (double)2.1f, "3");
+  sigc::internal::tuple_for_each<visitor_with_specializations>(t_original);
+}
 
 int
 main() {
@@ -215,5 +232,7 @@ main() {
 
   test_tuple_for_each_empty_tuple();
 
+  test_tuple_for_each_constexpr();
+
   return EXIT_SUCCESS;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]