Skip to content

Commit

Permalink
SFINAE out the converting constructor of arg<I> to avoid hard errors …
Browse files Browse the repository at this point in the history
…with is_convertible on g++ 4.8/4.9
  • Loading branch information
pdimov committed Aug 19, 2016
1 parent 2821b51 commit 3c56630
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
13 changes: 10 additions & 3 deletions include/boost/bind/arg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@

#include <boost/config.hpp>
#include <boost/is_placeholder.hpp>
#include <boost/static_assert.hpp>

namespace boost
{

template< int I, int J > struct _arg_eq
{
};

template< int I > struct _arg_eq< I, I>
{
typedef void type;
};

template< int I > struct arg
{
BOOST_CONSTEXPR arg()
{
}

template< class T > BOOST_CONSTEXPR arg( T const & /* t */ )
template< class T > BOOST_CONSTEXPR arg( T const & /* t */, typename _arg_eq< I, is_placeholder<T>::value >::type * = 0 )
{
BOOST_STATIC_ASSERT( I == is_placeholder<T>::value );
}
};

Expand Down
3 changes: 3 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ test-suite "bind"
[ run bind_type_test.cpp ]
[ run bind_unique_ptr_test.cpp ]
[ run bind_nested_rv_test.cpp ]
[ compile arg_copy_test.cpp ]
[ compile-fail arg_copy_fail.cpp ]
[ run placeholder_std_bind_test.cpp ]
;
19 changes: 19 additions & 0 deletions test/arg_copy_fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// arg_copy_fail.cpp - arg<1> to arg<2>
//
// Copyright 2016 Peter Dimov
//
// 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
//

#include <boost/bind/arg.hpp>

//

int main()
{
boost::arg<1> a1(( boost::arg<2>() ));
(void)a1;
}
34 changes: 34 additions & 0 deletions test/arg_copy_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// arg_copy_test.cpp - copying a custom placeholder _1 to arg<1>
//
// Copyright 2016 Peter Dimov
//
// 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
//

#include <boost/is_placeholder.hpp>
#include <boost/bind/arg.hpp>

//

template<int I> struct ph
{
};

namespace boost
{

template<int I> struct is_placeholder< ::ph<I> >
{
enum _vt { value = I };
};

} // namespace boost

int main()
{
boost::arg<1> a1 = ph<1>();
(void)a1;
}
46 changes: 46 additions & 0 deletions test/placeholder_std_bind_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// placeholder_std_bind_test.cpp - std::bind with Boost's _1
//
// Copyright 2016 Peter Dimov
//
// 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
//

#include <boost/config.hpp>

#if defined( BOOST_NO_CXX11_HDR_FUNCTIONAL )

int main()
{
}

#else

#include <boost/bind.hpp>
#include <boost/core/lightweight_test.hpp>
#include <functional>

namespace std
{

template<int N> struct is_placeholder< boost::arg<N> >: public integral_constant<int, N> {};

} // namespace std

int foo( int i )
{
return i;
}

int main()
{
BOOST_TEST_EQ( std::bind( foo, _1 )( 1 ), 1 );
BOOST_TEST_EQ( std::bind( foo, _2 )( 1, 2 ), 2 );
BOOST_TEST_EQ( std::bind( foo, _3 )( 1, 2, 3 ), 3 );

return boost::report_errors();
}

#endif

0 comments on commit 3c56630

Please sign in to comment.