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:
37
fit/detail/delegate.h
Normal file
37
fit/detail/delegate.h
Normal 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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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>
|
||||
|
||||
2
fit/on.h
2
fit/on.h
@@ -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 {
|
||||
|
||||
16
test/on.cpp
16
test/on.cpp
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user