Check number of arguments in bind expressions.

* include/std/functional (_Mem_fn_traits_base::__arity): New typedef.
	(_Mem_fn_base::_Arity): New typedef.
	(_Bind_check_arity): New class template.
	(_Bind_helper, _Bindres_helper, _Bind_simple_helper): Check arity.
	* testsuite/20_util/bind/ref_neg.cc: Adjust dg-error.

From-SVN: r217025
This commit is contained in:
Jonathan Wakely 2014-11-03 02:55:36 +00:00 committed by Jonathan Wakely
parent 8d9076969b
commit afc449e82c
3 changed files with 35 additions and 4 deletions

View File

@ -9,6 +9,12 @@
(_Mem_fn): Inherit from _Mem_fn_base.
* testsuite/20_util/function_objects/mem_fn/refqual.cc: New.
* include/std/functional (_Mem_fn_traits_base::__arity): New typedef.
(_Mem_fn_base::_Arity): New typedef.
(_Bind_check_arity): New class template.
(_Bind_helper, _Bindres_helper, _Bind_simple_helper): Check arity.
* testsuite/20_util/bind/ref_neg.cc: Adjust dg-error.
2014-10-31 Jonathan Wakely <jwakely@redhat.com>
* include/bits/stl_bvector.h (_Bvector_base): Use allocator_traits.

View File

@ -515,6 +515,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
using __arg_types = _Pack<_ArgTypes...>;
using __maybe_type
= _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...>;
using __arity = integral_constant<size_t, sizeof...(_ArgTypes)>;
};
template<typename _Res, typename _Class, typename... _ArgTypes>
@ -634,6 +635,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
public:
using result_type = typename _Traits::__result_type;
using _Arity = typename _Traits::__arity;
private:
using _Class = typename _Traits::__class_type;
@ -755,6 +757,8 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
{ return (*__ptr).*_M_pm; }
public:
using _Arity = integral_constant<size_t, 0>;
explicit
_Mem_fn_base(_Res _Class::*__pm) noexcept : _M_pm(__pm) { }
@ -1485,6 +1489,24 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
struct is_bind_expression<const volatile _Bind_result<_Result, _Signature>>
: public true_type { };
template<typename _Func, typename... _BoundArgs>
struct _Bind_check_arity { };
template<typename _Ret, typename... _Args, typename... _BoundArgs>
struct _Bind_check_arity<_Ret (*)(_Args...), _BoundArgs...>
{
static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
"Wrong number of arguments for function");
};
template<typename _Tp, typename _Class, typename... _BoundArgs>
struct _Bind_check_arity<_Tp _Class::*, _BoundArgs...>
{
using _Arity = typename _Mem_fn<_Tp _Class::*>::_Arity;
static_assert(sizeof...(_BoundArgs) == _Arity::value + 1,
"Wrong number of arguments for pointer-to-member");
};
// Trait type used to remove std::bind() from overload set via SFINAE
// when first argument has integer type, so that std::bind() will
// not be a better match than ::bind() from the BSD Sockets API.
@ -1493,6 +1515,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
template<bool _SocketLike, typename _Func, typename... _BoundArgs>
struct _Bind_helper
: _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
{
typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
__maybe_type;
@ -1525,6 +1548,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
template<typename _Result, typename _Func, typename... _BoundArgs>
struct _Bindres_helper
: _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
{
typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
__maybe_type;
@ -1599,6 +1623,7 @@ _GLIBCXX_HAS_NESTED_TYPE(result_type)
template<typename _Func, typename... _BoundArgs>
struct _Bind_simple_helper
: _Bind_check_arity<typename decay<_Func>::type, _BoundArgs...>
{
typedef _Maybe_wrap_member_pointer<typename decay<_Func>::type>
__maybe_type;

View File

@ -30,10 +30,10 @@ void test01()
{
const int dummy = 0;
std::bind(&inc, _1)(0); // { dg-error "no match" }
// { dg-error "rvalue|const" "" { target *-*-* } 1315 }
// { dg-error "rvalue|const" "" { target *-*-* } 1329 }
// { dg-error "rvalue|const" "" { target *-*-* } 1343 }
// { dg-error "rvalue|const" "" { target *-*-* } 1357 }
// { dg-error "rvalue|const" "" { target *-*-* } 1213 }
// { dg-error "rvalue|const" "" { target *-*-* } 1227 }
// { dg-error "rvalue|const" "" { target *-*-* } 1241 }
// { dg-error "rvalue|const" "" { target *-*-* } 1255 }
std::bind(&inc, std::ref(dummy))(); // { dg-error "no match" }
}