sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Upcasting of unique_usertype fails in a signature matching

Open niello opened this issue 2 years ago • 0 comments

I use unique_usertype_traits for a custom intrusive pointer Ptr.

template <typename T>
struct unique_usertype_traits<Ptr<T>>
{
	typedef T type;
	typedef Ptr<T> actual_type;
	static constexpr bool value = true;
	static bool is_null(const actual_type& ptr) { return !ptr; }
	static type* get(const actual_type& ptr) { return ptr.Get(); }
};

Then I bind a couple of functions, one to create a subclass of particular type and another to release it:

class Base {};
class SubclassA : public Base {};

class Factory
{
template<typename T> Ptr<T> Create() { return Ptr(new T()); }
void Release(Ptr<Base> p) { ... }
}

_Session->GetScriptState().new_usertype<Factory>("Factory"
	, "CreateSubclassA", &Factory::Create<SubclassA>
	, "Release", &Factory::Release
);

Then in Lua I do:

local x = MyFactory.CreateSubclassA()
MyFactory.Release(x)

and the call to Release fails. But if I change the binding to

	, "Release", [](Factory& self, Base* p) { self.Release(Ptr(p)); }

then everything works.

It seems that sol can't treat Ptr<Derived> as Ptr<Base> in an argument of a signature, although it can match Ptr<Derived> and Base*. Could you please fix this and enable direct binding in this case?

niello avatar Jan 12 '23 11:01 niello