2
0
mirror of https://github.com/boostorg/lambda2.git synced 2026-01-24 05:52:17 +00:00

Compare commits

..

5 Commits

Author SHA1 Message Date
Peter Dimov
2ff3fef3d1 Update documentation 2021-10-18 21:33:52 +03:00
Peter Dimov
0572e42219 Work around libc++ issue (again.) 2021-10-18 21:20:14 +03:00
Peter Dimov
7c39436198 Add first, second function objects. Fixes #5. 2021-10-18 20:57:25 +03:00
Peter Dimov
f8b3099259 Fix placeholder types in the synopsis 2021-09-05 19:01:20 +03:00
Peter Dimov
a666ac4fb6 Update revision history 2021-09-05 18:46:17 +03:00
5 changed files with 62 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ Peter Dimov
:toc: left :toc: left
:toclevels: 4 :toclevels: 4
:idprefix: :idprefix:
:listing-caption: Code Example
:docinfo: private-footer :docinfo: private-footer
:leveloffset: +1 :leveloffset: +1

View File

@@ -12,3 +12,5 @@ https://www.boost.org/LICENSE_1_0.txt
* Added special cases in `operator<<` and `operator>>` when * Added special cases in `operator<<` and `operator>>` when
the first argument is a stream, to allow `std::cout << _1`. the first argument is a stream, to allow `std::cout << _1`.
* Added `operator\->*`.
* Added `first`, `second`.

View File

@@ -21,14 +21,14 @@ namespace lambda2 {
template<int I> struct lambda2_arg; template<int I> struct lambda2_arg;
inline constexpr lambda2_arg<1> _1{}; inline constexpr lambda2_arg<1> _1{};
inline constexpr lambda2_arg<1> _2{}; inline constexpr lambda2_arg<2> _2{};
inline constexpr lambda2_arg<1> _3{}; inline constexpr lambda2_arg<3> _3{};
inline constexpr lambda2_arg<1> _4{}; inline constexpr lambda2_arg<4> _4{};
inline constexpr lambda2_arg<1> _5{}; inline constexpr lambda2_arg<5> _5{};
inline constexpr lambda2_arg<1> _6{}; inline constexpr lambda2_arg<6> _6{};
inline constexpr lambda2_arg<1> _7{}; inline constexpr lambda2_arg<7> _7{};
inline constexpr lambda2_arg<1> _8{}; inline constexpr lambda2_arg<8> _8{};
inline constexpr lambda2_arg<1> _9{}; inline constexpr lambda2_arg<9> _9{};
// arithmetic operators // arithmetic operators
@@ -90,6 +90,11 @@ template<class A, class B> auto operator>>=( A && a, B && b );
template<class A, class B> auto operator->*( A && a, B && b ); template<class A, class B> auto operator->*( A && a, B && b );
// projections
inline constexpr /unspecified/ first{};
inline constexpr /unspecified/ second{};
} // namespace lambda2 } // namespace lambda2
} // namespace boost } // namespace boost
``` ```
@@ -405,3 +410,26 @@ Returns: :: `std::bind( std::forward<B>(b), std::forward<A>(a) );`
Notes: :: This operator is intended to be used with "projection" function Notes: :: This operator is intended to be used with "projection" function
objects such as member pointers or member functions taking zero arguments, objects such as member pointers or member functions taking zero arguments,
as in `_1\->*&X::m` or `_1\->*&X::f`. as in `_1\->*&X::m` or `_1\->*&X::f`.
### Projections
```
inline constexpr /unspecified/ first{};
```
A function object such that `first(x)` returns `std::get<0>(x)`.
```
inline constexpr /unspecified/ second{};
```
A function object such that `second(x)` returns `std::get<1>(x)`.
.Using first and second to print out a map
```
void print( std::map<int, std::string> const & m )
{
using namespace boost::lambda2;
std::for_each( m.begin(), m.end(), std::cout << _1->*first << ": " << _1->*second << '\n' );
}
```

View File

@@ -32,6 +32,14 @@ struct subscript
} }
}; };
template<int I> struct get
{
template<class T> decltype(auto) operator()( T&& t ) const
{
return std::get<I>( std::forward<T>(t) );
}
};
} // namespace lambda2_detail } // namespace lambda2_detail
// placeholders // placeholders
@@ -65,6 +73,11 @@ BOOST_LAMBDA2_INLINE_VAR constexpr lambda2_arg<7> _7{};
BOOST_LAMBDA2_INLINE_VAR constexpr lambda2_arg<8> _8{}; BOOST_LAMBDA2_INLINE_VAR constexpr lambda2_arg<8> _8{};
BOOST_LAMBDA2_INLINE_VAR constexpr lambda2_arg<9> _9{}; BOOST_LAMBDA2_INLINE_VAR constexpr lambda2_arg<9> _9{};
// first, second
BOOST_LAMBDA2_INLINE_VAR constexpr lambda2_detail::get<0> first{};
BOOST_LAMBDA2_INLINE_VAR constexpr lambda2_detail::get<1> second{};
#undef BOOST_LAMBDA2_INLINE_VAR #undef BOOST_LAMBDA2_INLINE_VAR
} // namespace lambda2 } // namespace lambda2

View File

@@ -63,6 +63,16 @@ int main()
BOOST_TEST_EQ( (_1->*&std::pair<int, int>::first)( x ), 1 ); BOOST_TEST_EQ( (_1->*&std::pair<int, int>::first)( x ), 1 );
BOOST_TEST_EQ( (_1->*&std::pair<int, int>::second)( x ), 2 ); BOOST_TEST_EQ( (_1->*&std::pair<int, int>::second)( x ), 2 );
#if defined(_LIBCPP_VERSION)
// https://bugs.llvm.org/show_bug.cgi?id=51753
using std::placeholders::_1;
#endif
BOOST_TEST_EQ( (_1->*first)( x ), 1 );
BOOST_TEST_EQ( (_1->*second)( x ), 2 );
} }
return boost::report_errors(); return boost::report_errors();