Add operator[] to placeholders

This commit is contained in:
Peter Dimov 2021-06-27 17:05:54 +03:00
parent 07c965a088
commit 04367ac3e5
3 changed files with 46 additions and 0 deletions

View File

@ -20,6 +20,19 @@ namespace boost
namespace lambda2 namespace lambda2
{ {
namespace lambda2_detail
{
struct subscript
{
template<class T1, class T2> decltype(auto) operator()(T1&& t1, T2&& t2) const
{
return std::forward<T1>(t1)[ std::forward<T2>(t2) ];
}
};
} // namespace lambda2_detail
// placeholders // placeholders
template<int I> struct lambda2_arg template<int I> struct lambda2_arg
@ -28,6 +41,11 @@ template<int I> struct lambda2_arg
{ {
return std::get<std::size_t{I-1}>( std::tuple<A&&...>( std::forward<A>(a)... ) ); return std::get<std::size_t{I-1}>( std::tuple<A&&...>( std::forward<A>(a)... ) );
} }
template<class T> auto operator[]( T&& t ) const
{
return std::bind( lambda2_detail::subscript(), *this, std::forward<T>( t ) );
}
}; };
#if defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L #if defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L

View File

@ -22,3 +22,4 @@ run lookup_problem.cpp ;
run dereference.cpp ; run dereference.cpp ;
run placeholders.cpp ; run placeholders.cpp ;
run increment.cpp ; run increment.cpp ;
run subscript.cpp ;

27
test/subscript.cpp Normal file
View File

@ -0,0 +1,27 @@
// Copyright 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/lambda2.hpp>
#include <boost/core/lightweight_test.hpp>
#include <functional>
int main()
{
using namespace boost::lambda2;
int x[] = { 1, 2, 3, 4 };
BOOST_TEST_EQ( _1[0](x), x[0] );
BOOST_TEST_EQ( _1[_2](x, 1), x[1] );
BOOST_TEST_EQ( (_1[_2] + _3[_4])(x, 2, x, 3), x[2] + x[3] );
BOOST_TEST_EQ( std::bind(_1 + _2, _1[_2], _3[_4])(x, 2, x, 3), x[2] + x[3] );
_1[0](x) = 7;
BOOST_TEST_EQ( x[0], 7 );
_1[_2](x, 1) = 8;
BOOST_TEST_EQ( x[1], 8 );
return boost::report_errors();
}