Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for member function to member_ptr #449

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/boost/hana/detail/struct_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/hana/tuple.hpp>

#include <cstddef>
#include <type_traits>
#include <utility>


BOOST_HANA_NAMESPACE_BEGIN namespace struct_detail {
template <typename Memptr, Memptr ptr>
struct member_ptr {
template <typename T>
constexpr decltype(auto) operator()(T&& t) const
template <typename T, typename Memptr2 = Memptr, std::enable_if_t<std::is_member_object_pointer_v<Memptr2>, int> = 0>
constexpr decltype(auto) operator()(T && t) const
{ return static_cast<T&&>(t).*ptr; }

template <typename T, typename Memptr2 = Memptr, std::enable_if_t<std::is_member_function_pointer_v<Memptr2>, int> = 0>
constexpr decltype(auto) operator()(T && t) const
{ return [&](auto ... args) { return static_cast<T&&>(t).*ptr(args...); }; }
};

constexpr std::size_t strlen(char const* s) {
Expand Down
12 changes: 10 additions & 2 deletions include/boost/hana/detail/struct_macros.hpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,17 @@ Distributed under the Boost Software License, Version 1.0.
BOOST_HANA_NAMESPACE_BEGIN namespace struct_detail {
template <typename Memptr, Memptr ptr>
struct member_ptr {
template <typename T>
constexpr decltype(auto) operator()(T&& t) const
template <typename Memptr2, typename T, std::enable_if_t<std::is_member_object_pointer_v<Memptr2>, int> = 0>
constexpr decltype(auto) dispatch(T && t) const
{ return static_cast<T&&>(t).*ptr; }

template <typename Memptr2, typename T, std::enable_if_t<std::is_member_function_pointer_v<Memptr2>, int> = 0>
constexpr decltype(auto) dispatch(T && t) const
{ return [&](auto ... args) { return static_cast<T&&>(t).*ptr(args...); }; }

template <typename T>
constexpr decltype(auto) operator()(T && t) const
{ return dispatch<Memptr>(t); }
};

constexpr std::size_t strlen(char const* s) {
Expand Down
10 changes: 10 additions & 0 deletions test/concept/struct/macro.adapt_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,24 @@ namespace ns {
struct MemberArray {
int array[10];
};
struct MemberFunction {
int memfun(int x) { return x + 1; }
};
}

BOOST_HANA_ADAPT_STRUCT(ns::Data0);
BOOST_HANA_ADAPT_STRUCT(ns::Data1, member1);
BOOST_HANA_ADAPT_STRUCT(ns::Data2, member1, member2);
BOOST_HANA_ADAPT_STRUCT(ns::Data3, member1, member2, member3);
BOOST_HANA_ADAPT_STRUCT(ns::MemberArray, array);
BOOST_HANA_ADAPT_STRUCT(ns::MemberFunction, memfun);

static_assert(hana::Struct<ns::Data0>::value, "");
static_assert(hana::Struct<ns::Data1>::value, "");
static_assert(hana::Struct<ns::Data2>::value, "");
static_assert(hana::Struct<ns::Data3>::value, "");
static_assert(hana::Struct<ns::MemberArray>::value, "");
static_assert(hana::Struct<ns::MemberFunction>::value, "");

int main() {
BOOST_HANA_CONSTANT_CHECK(hana::contains(ns::Data1{}, BOOST_HANA_STRING("member1")));
Expand All @@ -55,4 +60,9 @@ int main() {
BOOST_HANA_CONSTANT_CHECK(hana::contains(ns::Data3{}, BOOST_HANA_STRING("member3")));

BOOST_HANA_CONSTANT_CHECK(hana::contains(ns::MemberArray{}, BOOST_HANA_STRING("array")));

BOOST_HANA_CONSTANT_CHECK(hana::contains(ns::MemberFunction{}, BOOST_HANA_STRING("memfun")));

constexpr auto accessors = hana::accessors<ns::MemberFunction>();
BOOST_HANA_RUNTIME_CHECK(hana::second(accessors[hana::size_c<0>])(ns::MemberFunction{})(1) == 2);
}