2
0
mirror of https://github.com/boostorg/hof.git synced 2026-01-31 20:22:11 +00:00

Add macros for delegating constructors

This commit is contained in:
Paul
2014-08-08 19:26:40 -04:00
parent a9e4362137
commit 3dcc2f3cdc
6 changed files with 65 additions and 17 deletions

37
fit/detail/delegate.h Normal file
View File

@@ -0,0 +1,37 @@
/*=============================================================================
Copyright (c) 2012 Paul Fultz II
delgate.h
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef FIT_GUARD_FUNCTION_DELGATE_H
#define FIT_GUARD_FUNCTION_DELGATE_H
#include <type_traits>
#include <utility>
#define FIT_ENABLE_IF_CONVERTIBLE(...) \
typename std::enable_if<std::is_convertible<__VA_ARGS__>::value, int>::type = 0
#define FIT_ENABLE_IF_CONVERTIBLE_UNPACK(...) \
typename std::enable_if<std::is_convertible<__VA_ARGS__>::value..., int>::type = 0
#define FIT_ENABLE_IF_CONSTRUCTIBLE(...) \
typename std::enable_if<std::is_constructible<__VA_ARGS__>::value, int>::type = 0
#define FIT_DELGATE_CONSTRUCTOR(C, T, var) \
template<class... FitXs, FIT_ENABLE_IF_CONSTRUCTIBLE(T, FitXs&&...)> \
constexpr C(FitXs&&... fit_xs) : var(std::forward<FitXs>(fit_xs)...) {}
// TODO: For compilers that support inheriting constructors replace with
// `using Base::Base;`
#define FIT_INHERIT_CONSTRUCTOR(Derived, Base) FIT_DELGATE_CONSTRUCTOR(Derived, Base, Base)
namespace fit {
namespace detail {
}
}
#endif

View File

@@ -1,16 +1,27 @@
/*=============================================================================
Copyright (c) 2014 Paul Fultz II
enable_if_convertible.h
remove_rvalue_reference.h
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef FIT_GUARD_FUNCTION_ENABLE_IF_CONVERTIBLE_H
#define FIT_GUARD_FUNCTION_ENABLE_IF_CONVERTIBLE_H
#ifndef FIT_GUARD_FUNCTION_REMOVE_RVALUE_REFERENCE_H
#define FIT_GUARD_FUNCTION_REMOVE_RVALUE_REFERENCE_H
#include <utility>
namespace fit { namespace detail {
#define FIT_ENABLE_IF_CONVERTIBLE(T, U) \
typename std::enable_if<std::is_convertible<T, U>::value, int>::type = 0
template<class T>
struct remove_rvalue_reference
{
typedef T type;
};
template<class T>
struct remove_rvalue_reference<T&&>
{
typedef T type;
};
}}
#endif

View File

@@ -32,7 +32,7 @@
#include <fit/always.h>
#include <fit/returns.h>
#include <fit/enable_if_convertible.h>
#include <fit/detail/delegate.h>
#ifndef FIT_FIX_HAS_CONSTEXPR
#define FIT_FIX_HAS_CONSTEXPR 0

View File

@@ -44,7 +44,7 @@
#include <fit/always.h>
#include <fit/static.h>
#include <fit/invoke.h>
#include <fit/enable_if_convertible.h>
#include <fit/detail/delegate.h>
#include <tuple>
#include <functional>
#include <type_traits>

View File

@@ -41,7 +41,7 @@
#include <utility>
#include <fit/always.h>
#include <fit/enable_if_convertible.h>
#include <fit/detail/delegate.h>
#include <fit/returns.h>
namespace fit {

View File

@@ -4,20 +4,20 @@
struct foo
{
constexpr foo(int x) : x(x)
{}
int x;
constexpr foo(int x) : x(x)
{}
int x;
};
struct select_x
{
template<class T>
constexpr auto operator()(T&& x) const FIT_RETURNS(x.x);
template<class T>
constexpr auto operator()(T&& x) const FIT_RETURNS(x.x);
};
FIT_TEST_CASE()
{
constexpr auto add = fit::_ + fit::_;
static_assert(fit::on(select_x(), fit::_ + fit::_)(foo(1), foo(2)) == 3, "Constexpr projection failed");
FIT_TEST_CHECK(fit::on(std::mem_fn(&foo::x), fit::_ + fit::_)(foo(1), foo(2)) == 3);
constexpr auto add = fit::_ + fit::_;
static_assert(fit::on(select_x(), fit::_ + fit::_)(foo(1), foo(2)) == 3, "Constexpr projection failed");
FIT_TEST_CHECK(fit::on(std::mem_fn(&foo::x), fit::_ + fit::_)(foo(1), foo(2)) == 3);
}