What is the purpose of a structure template without definition?

The following is a snippet from Boost.Python source code :

template <class T> struct null_ok; // how it working?

template <class T>
inline null_ok<T>* allow_null(T* p)
{
    return (null_ok<T>*)p;
}

He combined that there is no definition for the declared struct null_ok, but null_okhas nothing to do with the template argument T.

In the Python wiki, some tips:

handle <> y (null_ok (x)) allows y to become NULL

handle <> y (x) , where x is not the result of null_ok, never yields NULL y. An exception will be thrown if x is NULL

I can’t understand how a declaration (without a definition) of a structure template null_okcould achieve the goal mentioned above?

+4
2

, " null" .

, , , null_ok<T>*, ( T*).

null_ok, , - null_ok<int> a;.

+4

handle.hpp ( ):

// this is a borrowed handle which can be null,
// so increment refcount if p is not null
// (xincref just does nothing in this case)
template <class T>
inline T* manage_ptr(detail::borrowed<null_ok<T> >* p, int)
{
  return python::xincref((T*)p);
}

// the same - as stated in the doc, null_ok and borrowed are commutative
template <class T>
inline T* manage_ptr(null_ok<detail::borrowed<T> >* p, int)
{
  return python::xincref((T*)p);
}


// this is a borrowed handle which cannot be null, so make sure p is not null
// and increment refcount.
template <class T>
inline T* manage_ptr(detail::borrowed<T>* p, long)
{
  return python::incref(expect_non_null((T*)p));
}

// p is not borrowed and can be null - do nothing.
template <class T>
inline T* manage_ptr(null_ok<T>* p, long)
{
  return (T*)p;
}

// p is not borrowed and cannot be null - make sure it isn't null indeed.
template <class T>
inline T* manage_ptr(T* p, ...)
{
  return expect_non_null(p);
}

null_ok<T> , , . .

+1

All Articles