[libsigcplusplus] tuple_cdr(), tuple_start(), tuple_end(): Use of constexpr.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libsigcplusplus] tuple_cdr(), tuple_start(), tuple_end(): Use of constexpr.
- Date: Tue, 8 Mar 2016 13:55:18 +0000 (UTC)
commit 845f1e2c61640f3118f5e6ee0a3ebd9d043ee5d2
Author: Murray Cumming <murrayc murrayc com>
Date: Tue Mar 8 14:18:59 2016 +0100
tuple_cdr(), tuple_start(), tuple_end(): Use of constexpr.
sigc++/tuple-utils/tuple_cdr.h | 2 ++
sigc++/tuple-utils/tuple_end.h | 13 ++++++++++---
sigc++/tuple-utils/tuple_start.h | 7 +++++--
tests/test_tuple_cdr.cc | 15 +++++++++++++++
tests/test_tuple_end.cc | 19 +++++++++++++++++++
tests/test_tuple_start.cc | 19 +++++++++++++++++++
6 files changed, 70 insertions(+), 5 deletions(-)
---
diff --git a/sigc++/tuple-utils/tuple_cdr.h b/sigc++/tuple-utils/tuple_cdr.h
index eab31a6..c4d013a 100644
--- a/sigc++/tuple-utils/tuple_cdr.h
+++ b/sigc++/tuple-utils/tuple_cdr.h
@@ -41,6 +41,7 @@ struct tuple_type_cdr<std::tuple<H, T...>>
namespace detail {
template <typename T, std::size_t... I>
+constexpr
decltype(auto)
tuple_cdr_impl(T&& t, std::index_sequence<0, I...>)
{
@@ -55,6 +56,7 @@ tuple_cdr_impl(T&& t, std::index_sequence<0, I...>)
* This is analogous to std::tuple_cat().
*/
template <typename T>
+constexpr
decltype(auto)
tuple_cdr(T&& t) {
//We use std::decay_t<> because tuple_size is not defined for references.
diff --git a/sigc++/tuple-utils/tuple_end.h b/sigc++/tuple-utils/tuple_end.h
index 60637ae..890eddc 100644
--- a/sigc++/tuple-utils/tuple_end.h
+++ b/sigc++/tuple-utils/tuple_end.h
@@ -27,7 +27,9 @@ namespace detail {
template <typename T, std::size_t remove_from_start>
struct tuple_end_impl {
- static decltype(auto) // typename tuple_type_end<T, size - remove_from_start>::type
+ constexpr
+ static
+ decltype(auto) // typename tuple_type_end<T, size - remove_from_start>::type
tuple_end(T&& t) {
static_assert(remove_from_start > 0, "remove_from_start must be more than zero.");
@@ -39,7 +41,9 @@ struct tuple_end_impl {
template <typename T>
struct tuple_end_impl<T, 1> {
- static decltype(auto)
+ constexpr
+ static
+ decltype(auto)
tuple_end(T&& t) {
return tuple_cdr(std::forward<T>(t));
}
@@ -47,7 +51,9 @@ struct tuple_end_impl<T, 1> {
template <typename T>
struct tuple_end_impl<T, 0> {
- static decltype(auto)
+ constexpr
+ static
+ decltype(auto)
tuple_end(T&& t) {
return std::forward<T>(t);
}
@@ -59,6 +65,7 @@ struct tuple_end_impl<T, 0> {
* Get the tuple with the last @a len items of the original.
*/
template <std::size_t len, typename T>
+constexpr
decltype(auto) // typename tuple_type_end<T, len>::type
tuple_end(T&& t) {
//We use std::decay_t<> because tuple_size is not defined for references.
diff --git a/sigc++/tuple-utils/tuple_start.h b/sigc++/tuple-utils/tuple_start.h
index 04f738d..4e7d53c 100644
--- a/sigc++/tuple-utils/tuple_start.h
+++ b/sigc++/tuple-utils/tuple_start.h
@@ -50,7 +50,9 @@ struct tuple_start_impl;
template <typename T, std::size_t... I>
struct tuple_start_impl<T, std::index_sequence<I...>> {
- static decltype(auto)
+ static
+ constexpr
+ decltype(auto)
tuple_start(T&& t) {
constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
constexpr auto len = sizeof...(I);
@@ -67,8 +69,9 @@ struct tuple_start_impl<T, std::index_sequence<I...>> {
* Get the tuple with the last @a len items of the original.
*/
template <std::size_t len, typename T>
+constexpr
decltype(auto) // typename tuple_type_end<T, len>::type
- tuple_start(T&& t) {
+tuple_start(T&& t) {
//We use std::decay_t<> because tuple_size is not defined for references.
constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
static_assert(len <= size, "The tuple size must be less than or equal to the length.");
diff --git a/tests/test_tuple_cdr.cc b/tests/test_tuple_cdr.cc
index 12c9650..028a15f 100644
--- a/tests/test_tuple_cdr.cc
+++ b/tests/test_tuple_cdr.cc
@@ -66,11 +66,26 @@ test_tuple_cdr_stdref() {
assert(std::get<1>(t_suffix) == "world");
}
+constexpr
+void
+test_tuple_cdr_constexpr() {
+ constexpr auto str_hello = "hello";
+ constexpr auto str_world = "world";
+
+ constexpr auto t_larger =
+ std::make_tuple(nullptr, str_hello, str_world);
+ constexpr auto t_suffix = sigc::internal::tuple_cdr(t_larger);
+ assert(std::get<0>(t_suffix) == str_hello);
+ assert(std::get<1>(t_suffix) == str_world);
+}
+
int
main() {
test_tuple_type_cdr();
test_tuple_cdr();
test_tuple_cdr_stdref();
+ test_tuple_cdr_constexpr();
+
return EXIT_SUCCESS;
}
diff --git a/tests/test_tuple_end.cc b/tests/test_tuple_end.cc
index 8eb313b..66635f3 100644
--- a/tests/test_tuple_end.cc
+++ b/tests/test_tuple_end.cc
@@ -86,10 +86,29 @@ test_tuple_end_stdref() {
}
+constexpr
+void
+test_tuple_end_constexpr() {
+ constexpr auto str_hello = "hello";
+ constexpr auto str_world = "world";
+
+ constexpr auto t_original =
+ std::make_tuple(nullptr, str_hello, str_world);
+ constexpr auto t_suffix = sigc::internal::tuple_end<2>(t_original);
+
+ static_assert(std::tuple_size<decltype(t_suffix)>::value == 2,
+ "unexpected tuple_end()ed tuple size.");
+
+ assert(std::get<0>(t_suffix) == str_hello);
+ assert(std::get<1>(t_suffix) == str_world);
+}
+
int
main() {
test_tuple_end();
test_tuple_end_stdref();
+ test_tuple_end_constexpr();
+
return EXIT_SUCCESS;
}
diff --git a/tests/test_tuple_start.cc b/tests/test_tuple_start.cc
index 6d68714..3d136bf 100644
--- a/tests/test_tuple_start.cc
+++ b/tests/test_tuple_start.cc
@@ -115,9 +115,28 @@ test_tuple_start_stdref() {
assert(std::get<1>(t_prefix) == "world");
}
+constexpr
+void
+test_tuple_start_constexpr() {
+ constexpr auto str_hello = "hello";
+ constexpr auto str_world = "hello";
+
+ constexpr auto t_original =
+ std::make_tuple(nullptr, str_hello, str_world);
+ constexpr auto t_prefix = sigc::internal::tuple_start<2>(t_original);
+
+ static_assert(std::tuple_size<decltype(t_prefix)>::value == 2,
+ "unexpected tuple_start()ed tuple size.");
+
+ assert(std::get<0>(t_prefix) == nullptr);
+ assert(std::get<1>(t_prefix) == str_hello);
+}
+
int
main() {
test_tuple_type_start();
test_tuple_start();
test_tuple_start_stdref();
+
+ test_tuple_start_constexpr();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]