[C++-sig] test if object instance is instance of extension class in C++

Holger Joukl Holger.Joukl at LBBW.de
Fri Feb 24 17:45:42 CET 2012


> > bp::object itself has reference (smart pointer) semantics, so there is
> > no need to pass objects by reference.
> > I see. Just out of curiosity:
> > If I pass by reference I do save a copy constructor call, don't I?
>
> In principle, yes. I'm not sure how much of this the compiler would be
> able to optimize away, though.
>
> > But I circumvent the bp::object refcount safety, though(?).
>
> Well, yes, but you don't need that as C++ language semantics ensure the
> object lifetime.
>
> > But in a type check like above this shouldn't be dangerous - right?
>
> There is definitely no danger in doing this. It may just add a little
> bit of overhead (since in certain cases passing by value is cheaper than
> passing by reference, depending on the involved types and optimization
> level.

Ok, this sounds like its best to do

inline
bool isMyExtensionClass(PyObject* pyobj) {
    bp::extract<MyExtensionClass&> extractor(pyobj);
    return extractor.check();
}


inline
bool isMyExtensionClass(bp::object obj) {
    bp::extract<MyExtensionClass&> extractor(obj);
    return extractor.check();
}

Many thanks again Stefan,
Holger

Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart



More information about the Cplusplus-sig mailing list