Skip to content

Commit

Permalink
Add boost::compat::invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Mar 21, 2024
1 parent 0220855 commit b685827
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/boost/compat/detail/returns.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef BOOST_COMPAT_DETAIL_RETURNS_HPP_INCLUDED
#define BOOST_COMPAT_DETAIL_RETURNS_HPP_INCLUDED

// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#define BOOST_COMPAT_RETURNS(...) noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) { return __VA_ARGS__; }

#endif // BOOST_COMPAT_DETAIL_RETURNS_HPP_INCLUDED
26 changes: 26 additions & 0 deletions include/boost/compat/invoke.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef BOOST_COMPAT_INVOKE_HPP_INCLUDED
#define BOOST_COMPAT_INVOKE_HPP_INCLUDED

// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/compat/mem_fn.hpp>
#include <boost/compat/detail/returns.hpp>
#include <utility>

namespace boost {
namespace compat {

template<class F, class... A>
auto invoke( F&& f, A&&... a )
BOOST_COMPAT_RETURNS( std::forward<F>(f)(std::forward<A>(a)...) )

template<class M, class T, class... A>
auto invoke( M T::* pm, A&&... a )
BOOST_COMPAT_RETURNS( mem_fn(pm)(std::forward<A>(a)...) )

} // namespace compat
} // namespace boost

#endif // BOOST_COMPAT_INVOKE_HPP_INCLUDED
18 changes: 18 additions & 0 deletions include/boost/compat/mem_fn.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef BOOST_COMPAT_MEM_FN_HPP_INCLUDED
#define BOOST_COMPAT_MEM_FN_HPP_INCLUDED

// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <functional>

namespace boost {
namespace compat {

using std::mem_fn;

} // namespace compat
} // namespace boost

#endif // BOOST_COMPAT_MEM_FN_HPP_INCLUDED
3 changes: 3 additions & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ project
run quick.cpp ;

run latch_test.cpp ;

run shared_lock_test.cpp ;
run shared_lock_test.cpp : : : <exception-handling>off : shared_lock_test_nx ;

run invoke_fn_test.cpp ;
36 changes: 36 additions & 0 deletions test/invoke_fn_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/compat/invoke.hpp>
#include <boost/core/lightweight_test.hpp>

int f0()
{
return -1;
}

int f1( int x1 )
{
return x1;
}

int f2( int x1, int x2 )
{
return 10*x1+x2;
}

int f3( int x1, int x2, int x3 )
{
return 100*x1 + 10*x2 + x3;
}

int main()
{
BOOST_TEST_EQ( boost::compat::invoke( f0 ), -1 );
BOOST_TEST_EQ( boost::compat::invoke( f1, 1 ), 1 );
BOOST_TEST_EQ( boost::compat::invoke( f2, 1, 2 ), 12 );
BOOST_TEST_EQ( boost::compat::invoke( f3, 1, 2, 3 ), 123 );

return boost::report_errors();
}

0 comments on commit b685827

Please sign in to comment.