Skip to content

Commit

Permalink
Add member function test for struct concept
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebionne committed Jun 14, 2019
1 parent daa802d commit 3cc1a71
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/boost/hana/detail/struct_macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ BOOST_HANA_NAMESPACE_BEGIN namespace struct_detail {

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); }; }
{ return [&](auto ... args) { return static_cast<T&&>(t).*ptr(args...); }; }

template <typename T>
constexpr decltype(auto) operator()(T && t) const
Expand Down
2 changes: 1 addition & 1 deletion include/boost/hana/detail/struct_macros.hpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ BOOST_HANA_NAMESPACE_BEGIN namespace struct_detail {

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); }; }
{ return [&](auto ... args) { return static_cast<T&&>(t).*ptr(args...); }; }

template <typename T>
constexpr decltype(auto) operator()(T && t) const
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);
}

0 comments on commit 3cc1a71

Please sign in to comment.