Skip to content

Commit 9bef38d

Browse files
committed
wip: C++20: Make sigc::ptr_fun() constexpr
This needs C++20 because it needs constexpr std::invoke(). However, I don't currently have a compiler/library that has constexpr std::invoke().
1 parent 2815cb3 commit 9bef38d

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

sigc++/functors/ptr_fun.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ class pointer_functor<T_return(T_args...)>
8282
public:
8383

8484
/// Constructs an invalid functor.
85-
pointer_functor() = default;
85+
constexpr pointer_functor() = default;
8686

8787
/** Constructs a pointer_functor2 object that wraps an existing function.
8888
* @param func Pointer to function that will be invoked from operator()().
8989
*/
90-
explicit pointer_functor(function_type func) : func_ptr_(func) {}
90+
constexpr explicit pointer_functor(function_type func) : func_ptr_(func) {}
9191

9292
/** Execute the wrapped function.
9393
* @param a Arguments to be passed on to the function.
9494
* @return The return value of the function invocation.
9595
*/
96-
T_return operator()(type_trait_take_t<T_args>... a) const {
96+
constexpr T_return operator()(type_trait_take_t<T_args>... a) const {
9797
return std::invoke(func_ptr_, a...);
9898
}
9999
};
@@ -105,7 +105,7 @@ class pointer_functor<T_return(T_args...)>
105105
* @ingroup ptr_fun
106106
*/
107107
template <typename T_return, typename... T_args>
108-
inline decltype(auto) ptr_fun(T_return (*func)(T_args...))
108+
inline constexpr decltype(auto) ptr_fun(T_return (*func)(T_args...))
109109
{
110110
return pointer_functor<T_return(T_args...)>(func);
111111
}

sigc++/functors/slot.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace internal
4848
* (argument-dependent lookup).
4949
*/
5050
template <typename T_out, typename T_in>
51-
inline T_out function_pointer_cast(T_in in)
51+
inline constexpr T_out function_pointer_cast(T_in in)
5252
{
5353
// The double reinterpret_cast suppresses a warning from gcc8 with the
5454
// -Wcast-function-type option.
@@ -78,13 +78,13 @@ struct typed_slot_rep : public slot_rep
7878
* The notification callback is registered using visit_each().
7979
* @param functor The functor contained by the new slot_rep object.
8080
*/
81-
inline explicit typed_slot_rep(const T_functor& functor)
81+
inline constexpr explicit typed_slot_rep(const T_functor& functor)
8282
: slot_rep(nullptr), functor_(std::make_unique<adaptor_type>(functor))
8383
{
8484
sigc::visit_each_trackable(slot_do_bind(this), *functor_);
8585
}
8686

87-
inline typed_slot_rep(const typed_slot_rep& src)
87+
inline constexpr typed_slot_rep(const typed_slot_rep& src)
8888
: slot_rep(src.call_), functor_(std::make_unique<adaptor_type>(*src.functor_))
8989
{
9090
sigc::visit_each_trackable(slot_do_bind(this), *functor_);

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set (TEST_SOURCE_FILES
2626
test_bind_return.cc
2727
test_compose.cc
2828
test_connection.cc
29+
test_constexpr.cc
2930
test_copy_invalid_slot.cc
3031
test_cpp11_lambda.cc
3132
test_custom.cc

tests/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ check_PROGRAMS = \
3333
test_bind_return \
3434
test_compose \
3535
test_connection \
36+
test_constexpr \
3637
test_copy_invalid_slot \
3738
test_cpp11_lambda \
3839
test_custom \
@@ -77,6 +78,7 @@ test_bind_refptr_SOURCES = test_bind_refptr.cc $(sigc_test_util)
7778
test_bind_return_SOURCES = test_bind_return.cc $(sigc_test_util)
7879
test_compose_SOURCES = test_compose.cc $(sigc_test_util)
7980
test_connection_SOURCES = test_connection.cc $(sigc_test_util)
81+
test_constexpr_SOURCES = test_constexpr.cc $(sigc_test_util)
8082
test_copy_invalid_slot_SOURCES = test_copy_invalid_slot.cc $(sigc_test_util)
8183
test_cpp11_lambda_SOURCES = test_cpp11_lambda.cc $(sigc_test_util)
8284
test_custom_SOURCES = test_custom.cc $(sigc_test_util)

tests/test_constexpr.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright 2002 - 2016, The libsigc++ Development Team
2+
* Assigned to public domain. Use as you wish without restriction.
3+
*/
4+
5+
#include "testutilities.h"
6+
#include <sigc++/sigc++.h>
7+
8+
namespace {
9+
10+
constexpr
11+
int foo(int a, int b) {
12+
return a + b;
13+
}
14+
15+
} // end anonymous namespace
16+
17+
int
18+
main()
19+
{
20+
constexpr auto slot = sigc::ptr_fun(&foo);
21+
constexpr auto result = slot(1, 2);
22+
if (result != 3) {
23+
return EXIT_FAILURE;
24+
}
25+
26+
EXIT_SUCCESS;
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy