Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

C++ Boost



A dtype is an object describing the type of the elements of an ndarray

<boost/python/numpy/dtype.hpp> contains the method calls necessary to generate a python object equivalent to a numpy.dtype from builtin C++ objects, as well as to create custom dtypes from user defined types


namespace boost
namespace python
namespace numpy

class dtype : public object
  static python::detail::new_reference convert(object::object_cref arg, bool align);

  // Convert an arbitrary Python object to a data-type descriptor object.
  template <typename T>
  explicit dtype(T arg, bool align=false);

  // Get the built-in numpy dtype associated with the given scalar template type.
  template <typename T> static dtype get_builtin();

  // Return the size of the data type in bytes.
  int get_itemsize() const;



template <typename T>
explicit dtype(T arg, bool align=false)

T must be either :

  • a built-in C++ typename convertible to object
  • a valid python object or convertible to object

Constructs an object from the supplied python object / convertible to object / builtin C++ data type



template <typename T> static dtype get_builtin();
Requirements:The typename supplied, T must be a builtin C++ type also supported by numpy
Returns:Numpy dtype corresponding to builtin C++ type


int get_itemsize() const;
Returns:the size of the data type in bytes.


namespace p = boost::python;
namespace np = boost::python::numpy;

np::dtype dtype = np::dtype::get_builtin<double>();
p::tuple for_custom_dtype = p::make_tuple("ha",dtype);
np::dtype custom_dtype = np::dtype(list_for_dtype);