summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMurray Cumming <murrayc@murrayc.com>2019-10-27 15:22:06 +0100
committerMurray Cumming <murrayc@murrayc.com>2019-10-27 15:28:00 +0100
commit9bef38d9a3eef6cfa859770b46e07ff22d64f4f6 (patch)
treea642754ec24dcffdfbbf74683ee0f773a3e2c968
parent2815cb3cea99a952e7a9bfde0c30a73330bbaeb6 (diff)
downloadsigc++-c++20.tar.gz
wip: C++20: Make sigc::ptr_fun() constexprc++20
This needs C++20 because it needs constexpr std::invoke(). However, I don't currently have a compiler/library that has constexpr std::invoke().
-rw-r--r--sigc++/functors/ptr_fun.h8
-rw-r--r--sigc++/functors/slot.h6
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/test_constexpr.cc27
5 files changed, 37 insertions, 7 deletions
diff --git a/sigc++/functors/ptr_fun.h b/sigc++/functors/ptr_fun.h
index c680a2e..bef305c 100644
--- a/sigc++/functors/ptr_fun.h
+++ b/sigc++/functors/ptr_fun.h
@@ -82,18 +82,18 @@ protected:
public:
/// Constructs an invalid functor.
- pointer_functor() = default;
+ constexpr pointer_functor() = default;
/** Constructs a pointer_functor2 object that wraps an existing function.
* @param func Pointer to function that will be invoked from operator()().
*/
- explicit pointer_functor(function_type func) : func_ptr_(func) {}
+ constexpr explicit pointer_functor(function_type func) : func_ptr_(func) {}
/** Execute the wrapped function.
* @param a Arguments to be passed on to the function.
* @return The return value of the function invocation.
*/
- T_return operator()(type_trait_take_t<T_args>... a) const {
+ constexpr T_return operator()(type_trait_take_t<T_args>... a) const {
return std::invoke(func_ptr_, a...);
}
};
@@ -105,7 +105,7 @@ public:
* @ingroup ptr_fun
*/
template <typename T_return, typename... T_args>
-inline decltype(auto) ptr_fun(T_return (*func)(T_args...))
+inline constexpr decltype(auto) ptr_fun(T_return (*func)(T_args...))
{
return pointer_functor<T_return(T_args...)>(func);
}
diff --git a/sigc++/functors/slot.h b/sigc++/functors/slot.h
index 5cda9cc..6befc02 100644
--- a/sigc++/functors/slot.h
+++ b/sigc++/functors/slot.h
@@ -48,7 +48,7 @@ namespace internal
* (argument-dependent lookup).
*/
template <typename T_out, typename T_in>
-inline T_out function_pointer_cast(T_in in)
+inline constexpr T_out function_pointer_cast(T_in in)
{
// The double reinterpret_cast suppresses a warning from gcc8 with the
// -Wcast-function-type option.
@@ -78,13 +78,13 @@ public:
* The notification callback is registered using visit_each().
* @param functor The functor contained by the new slot_rep object.
*/
- inline explicit typed_slot_rep(const T_functor& functor)
+ inline constexpr explicit typed_slot_rep(const T_functor& functor)
: slot_rep(nullptr), functor_(std::make_unique<adaptor_type>(functor))
{
sigc::visit_each_trackable(slot_do_bind(this), *functor_);
}
- inline typed_slot_rep(const typed_slot_rep& src)
+ inline constexpr typed_slot_rep(const typed_slot_rep& src)
: slot_rep(src.call_), functor_(std::make_unique<adaptor_type>(*src.functor_))
{
sigc::visit_each_trackable(slot_do_bind(this), *functor_);
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 36e728b..80d611d 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -26,6 +26,7 @@ set (TEST_SOURCE_FILES
test_bind_return.cc
test_compose.cc
test_connection.cc
+ test_constexpr.cc
test_copy_invalid_slot.cc
test_cpp11_lambda.cc
test_custom.cc
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 735d76b..39aecfa 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -33,6 +33,7 @@ check_PROGRAMS = \
test_bind_return \
test_compose \
test_connection \
+ test_constexpr \
test_copy_invalid_slot \
test_cpp11_lambda \
test_custom \
@@ -77,6 +78,7 @@ test_bind_refptr_SOURCES = test_bind_refptr.cc $(sigc_test_util)
test_bind_return_SOURCES = test_bind_return.cc $(sigc_test_util)
test_compose_SOURCES = test_compose.cc $(sigc_test_util)
test_connection_SOURCES = test_connection.cc $(sigc_test_util)
+test_constexpr_SOURCES = test_constexpr.cc $(sigc_test_util)
test_copy_invalid_slot_SOURCES = test_copy_invalid_slot.cc $(sigc_test_util)
test_cpp11_lambda_SOURCES = test_cpp11_lambda.cc $(sigc_test_util)
test_custom_SOURCES = test_custom.cc $(sigc_test_util)
diff --git a/tests/test_constexpr.cc b/tests/test_constexpr.cc
new file mode 100644
index 0000000..98e773b
--- /dev/null
+++ b/tests/test_constexpr.cc
@@ -0,0 +1,27 @@
+/* Copyright 2002 - 2016, The libsigc++ Development Team
+ * Assigned to public domain. Use as you wish without restriction.
+ */
+
+#include "testutilities.h"
+#include <sigc++/sigc++.h>
+
+namespace {
+
+constexpr
+int foo(int a, int b) {
+ return a + b;
+}
+
+} // end anonymous namespace
+
+int
+main()
+{
+ constexpr auto slot = sigc::ptr_fun(&foo);
+ constexpr auto result = slot(1, 2);
+ if (result != 3) {
+ return EXIT_FAILURE;
+ }
+
+ EXIT_SUCCESS;
+}