forked from boostorg/function
function_template.hpp:
- function partial specialization now allows assignment to zero (for clearing) and comparison against zero (for the empty check) (Brad King) function_test.cpp: - Check comparison against zero - Check assignment to zero function_test_fail1.cpp: function_test_fail2.cpp: - Make them fail for the right reasons [SVN r15803]
This commit is contained in:
@ -533,6 +533,11 @@ class function<BOOST_FUNCTION_PARTIAL_SPEC, Allocator>
|
|||||||
BOOST_FUNCTION_COMMA Allocator> base_type;
|
BOOST_FUNCTION_COMMA Allocator> base_type;
|
||||||
typedef function self_type;
|
typedef function self_type;
|
||||||
|
|
||||||
|
struct clear_type {};
|
||||||
|
|
||||||
|
class holder;
|
||||||
|
friend class holder;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef typename base_type::allocator_type allocator_type;
|
typedef typename base_type::allocator_type allocator_type;
|
||||||
|
|
||||||
@ -543,26 +548,44 @@ public:
|
|||||||
|
|
||||||
function(const self_type& f) : base_type(static_cast<const base_type&>(f)){}
|
function(const self_type& f) : base_type(static_cast<const base_type&>(f)){}
|
||||||
|
|
||||||
template<typename Functor>
|
self_type& operator=(self_type& f)
|
||||||
self_type& operator=(Functor BOOST_FUNCTION_TARGET_FIX(const &) f)
|
|
||||||
{
|
{
|
||||||
self_type(f).swap(*this);
|
self_type(f).swap(*this);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
self_type& operator=(const base_type& f)
|
inline self_type& operator=(holder h);
|
||||||
{
|
|
||||||
self_type(f).swap(*this);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
self_type& operator=(const self_type& f)
|
self_type& operator=(clear_type*)
|
||||||
{
|
{
|
||||||
self_type(f).swap(*this);
|
this->clear();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename R BOOST_FUNCTION_COMMA
|
||||||
|
BOOST_FUNCTION_TEMPLATE_PARMS,
|
||||||
|
typename Allocator>
|
||||||
|
class function<BOOST_FUNCTION_PARTIAL_SPEC, Allocator>::holder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
template<typename F> holder(F f) : func(f) {}
|
||||||
|
holder(const base_type& f) : func(f) {}
|
||||||
|
holder(const self_type& f) : func(f) {}
|
||||||
|
|
||||||
|
self_type func;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R BOOST_FUNCTION_COMMA
|
||||||
|
BOOST_FUNCTION_TEMPLATE_PARMS,
|
||||||
|
typename Allocator>
|
||||||
|
function<BOOST_FUNCTION_PARTIAL_SPEC, Allocator>&
|
||||||
|
function<BOOST_FUNCTION_PARTIAL_SPEC, Allocator>::operator=(holder h)
|
||||||
|
{
|
||||||
|
h.func.swap(*this);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
#undef BOOST_FUNCTION_PARTIAL_SPEC
|
#undef BOOST_FUNCTION_PARTIAL_SPEC
|
||||||
#endif // have partial specialization
|
#endif // have partial specialization
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ test_zero_args()
|
|||||||
|
|
||||||
// Assignment to an empty function
|
// Assignment to an empty function
|
||||||
v1 = five;
|
v1 = five;
|
||||||
BOOST_TEST(!v1.empty());
|
BOOST_TEST(v1 != 0);
|
||||||
|
|
||||||
// Invocation of a function
|
// Invocation of a function
|
||||||
global_int = 0;
|
global_int = 0;
|
||||||
@ -76,7 +76,7 @@ test_zero_args()
|
|||||||
|
|
||||||
// clear() method
|
// clear() method
|
||||||
v1.clear();
|
v1.clear();
|
||||||
BOOST_TEST(v1.empty());
|
BOOST_TEST(v1 == 0);
|
||||||
|
|
||||||
// Assignment to an empty function
|
// Assignment to an empty function
|
||||||
v1 = three;
|
v1 = three;
|
||||||
@ -97,13 +97,13 @@ test_zero_args()
|
|||||||
v1();
|
v1();
|
||||||
BOOST_TEST(global_int == 5);
|
BOOST_TEST(global_int == 5);
|
||||||
|
|
||||||
// clear()
|
// clear
|
||||||
v1.clear();
|
v1 = 0;
|
||||||
BOOST_TEST(v1.empty());
|
BOOST_TEST(0 == v1);
|
||||||
|
|
||||||
// Assignment to an empty function from a free function
|
// Assignment to an empty function from a free function
|
||||||
v1 = BOOST_FUNCTION_TARGET_FIX(&) write_five;
|
v1 = BOOST_FUNCTION_TARGET_FIX(&) write_five;
|
||||||
BOOST_TEST(!v1.empty());
|
BOOST_TEST(0 != v1);
|
||||||
|
|
||||||
// Invocation
|
// Invocation
|
||||||
global_int = 0;
|
global_int = 0;
|
||||||
|
@ -23,8 +23,8 @@ using namespace boost;
|
|||||||
int
|
int
|
||||||
test_main(int, char*[])
|
test_main(int, char*[])
|
||||||
{
|
{
|
||||||
function<int> f1;
|
function0<int> f1;
|
||||||
function<int> f2;
|
function0<int> f2;
|
||||||
|
|
||||||
if (f1 == f2) {
|
if (f1 == f2) {
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ static int bad_fn(float f) { return static_cast<int>(f); }
|
|||||||
int
|
int
|
||||||
test_main(int, char*[])
|
test_main(int, char*[])
|
||||||
{
|
{
|
||||||
function<int> f1;
|
function0<int> f1;
|
||||||
f1 = bad_fn;
|
f1 = bad_fn;
|
||||||
|
|
||||||
BOOST_CRITICAL_ERROR("This should not have compiled.");
|
BOOST_CRITICAL_ERROR("This should not have compiled.");
|
||||||
|
Reference in New Issue
Block a user