Skip to main content

C++/Python Type Mapping

dict

We map Python's dict to C++'s dict:

#ifndef CUSTOM_DICT
using dict = std::shared_ptr<std::unordered_map<std::string, ipipe::any>>;
#else
// todo : custom dict with bidirectional binding between C++ and Python
#endif

C++ to Python

Basic Type Conversion

cpppythonRemarks
std::stringbytes
cv::Matnumpy.ndarray
at::Tensortorch.Tensor
intint
floatfloat
doublefloat
unsigned intint
charstrInherited from Pybind11.
unsigned charint
boolbool

Container Conversion

For containers or nested containers, the conversion rules are as follows (where T is the basic type from the above table):

cpppythonRemarks
std::unordered_map<std::string, T>dict[str, T]
std::unordered_set<T>set[T]T must be hashable
std::vector<T>list[T]T is not an algebraic type
std::vector<T>numpy.ndarray[T]T is an algebraic type and not bool
std::vector<std::vector<T>>list[numpy.ndarray[T]]T is an algebraic type and not bool
std::map<std::string, T>Not supported
std::set<T>Not supported

Nested Types

The conversion of any to Python types also supports the following nested types:

cpppythonRemarks
std::vector<any>list[Any]Can be nested infinitely
std::unordered_map<std::string, any>dict[str, Any]Can be nested infinitely, 'data' key will be ignored

C++ Wrapper

For non-containers that fail to convert using the above rules, a custom type will be returned to Python:

cpppythonRemarks
anytorchpipe.any

Python to C++

Basic Type Conversion

pythoncppRemarks
strstd::string
bytesstd::string
numpy.ndarrayNot supported
torch.Tensorat::Tensor
boolbool
floatfloatprecision loss
intint

Container Conversion

For containers or nested containers, the conversion table is as follows (where T is the basic type from the above table):

pythoncppRemarks
dict[str, T]std::unordered_map<std::string, T>
set[T]std::unordered_set<T>T must be hashable, i.e. T != torch.Tensor
list[T]std::vector<T>

When the container on the Python side is empty, the C++ side cannot perceive the specific type. In this case, the framework will automatically handle such situations in a compatible way, so there is no need to pay extra attention to this scenario when using it.