Library of Assembled Shared Sources
lass::python::Self< T > Class Template Reference

Wrapper to type-hint a return value as Self. More...

#include <self.h>

Public Member Functions

T & reference ()
 Return reference to wrapped value.
 
const T & reference () const
 Return reference to wrapped value.
 

Detailed Description

template<typename T>
class lass::python::Self< T >

Wrapper to type-hint a return value as Self.

This is helpful for virtual methods that return an object of the same type as the this pointer. Here's an example with a clone method:

class Spam
{
public:
Self<TSpamPtr> clone() const
{
auto result = doClone();
const Spam* cloned = result.get();
LASS_ENFORCE(typeid(*cloned) != typeid(*this));
return result;
}
private:
virtual TSpamPtr doClone() const // must be overriden by each derived class
{
return TSpamPtr(new Spam(*this));
}
};

This will make sure the type-hint of Spam will look like:

class Spam:
def clone(self) -> Self: ...

See typing.Self and PEP 673 for more info.

See also
PyExportTraits<Self<T>>
NoNone
MaybeNone

Definition at line 91 of file self.h.


The documentation for this class was generated from the following file: