|
|
От: | FrozenHeart | |
| Дата: | 30.05.13 19:55 | ||
| Оценка: | |||
6 Lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed
on the second and third operands. After those conversions, one of the following shall hold:
[...]
— The second and third operands have pointer type, or one has pointer type and the other is a null
pointer constant, or both are null pointer constants, at least one of which is non-integral; pointer
conversions (4.10) and qualification conversions (4.4) are performed to bring them to their composite
pointer type (5.9). The result is of the composite pointer type.
2 [...] If one operand is a null pointer constant, the composite
pointer type is std::nullptr_t if the other operand is also a null pointer constant or, if the other operand
is a pointer, the type of the other operand.
#include <functional>
#include <iostream>
int main()
{
typedef void (*fp_t)();
fp_t fp = nullptr;
std::function<void()> f = fp;
std::cout << (f == nullptr) << '\n';
}template <class R, class... ArgTypes>
bool operator==(const function<R(ArgTypes...)>& f, nullptr_t) noexcept;
template <class R, class... ArgTypes>
bool operator==(nullptr_t, const function<R(ArgTypes...)>& f) noexcept;
1 Returns: !f.
template<class F> function(F f);
template <class F, class A> function(allocator_arg_t, const A& a, F f);
[...]
8 Postconditions: !*this if any of the following hold:
— f is a NULL function pointer.
— f is a NULL pointer to member.
— F is an instance of the function class template, and !f
template<typename _Signature>
static bool
_M_not_empty_function(const function<_Signature>& __f)
{ return static_cast<bool>(__f); }
template<typename _Tp>
static bool
_M_not_empty_function(const _Tp*& __fp)
{ return __fp; }
template<typename _Class, typename _Tp>
static bool
_M_not_empty_function(_Tp _Class::* const& __mp)
{ return __mp; }
template<typename _Tp>
static bool
_M_not_empty_function(const _Tp&)
{ return true; }template<typename _Tp>
static bool
_M_not_empty_function(const _Tp*& __fp)
{ return __fp; }template<typename _Tp>
static bool
_M_not_empty_function(const _Tp&)
{ return true; }#include <boost/function.hpp>
#include <iostream>
int main()
{
typedef void (*fp_t)();
fp_t fp = nullptr;
boost::function<void()> f = fp;
std::cout << (f == nullptr) << '\n';
}template<typename F> function(F f);
Requires:
F is a function object Callable from this.
Postconditions:
*this targets a copy of f if f is nonempty, or this->empty() if f is empty.
#include <boost/function.hpp>
#include <iostream>
typedef boost::function<void(int)> fp_t;
void bar(int) {}
void baz(fp_t fp)
{
if (!fp.empty())
{
std::cout << "true \n";
fp(0);
}
else
{
std::cout << "false \n";
}
}
int main()
{
bool foo = false;
baz(foo ? &bar : nullptr);
}#include <boost/function.hpp>
#include <iostream>
typedef boost::function<void(int)> fp_t;
void bar(int) {}
void baz(fp_t fp)
{
if (fp)
{
std::cout << "true \n";
fp(0);
}
else
{
std::cout << "false \n";
}
}
int main()
{
bool foo = false;
baz(foo ? &bar : nullptr);
}operator safe_bool() const;
Returns:
A safe_bool that evaluates false in a boolean context when this->empty(), and true otherwise.
Throws:
Will not throw.