Skip to content

Using std::shared_ptr as table keys? #1737

@Leadwerks

Description

@Leadwerks

I am having trouble getting tables to recognize that different STL shared pointers to the same object are the same object:

-- This will not remove the key
self.manager.zombies[self] = nil

-- This will remove the key
for k, v in pairs(self.manager.zombies) do
	if k == self then
		self.manager.zombies[k] = nil
	end
end

I tried declaring all of these meta functions in the class definition, but to no avail:

			sol::meta_function::equal_to, sol::overload(
				[](Model& self, Model* other) { return &self == other; },
				[](Model& self, Entity* other) { return &self == other; },
				[](Model& self, Object* other) { return &self == other; }
			),
			sol::meta_function::equal_to, sol::overload(
				[](Model& self, shared_ptr<Model> other) { return &self == other.get(); },
				[](Model& self, shared_ptr<Entity> other) { return &self == other.get(); },
				[](Model& self, shared_ptr<Object> other) { return &self == other.get(); }
			),
			sol::meta_function::less_than, sol::overload(
				[](Model& self, Model* other) { return &self < other; },
				[](Model& self, Entity* other) { return &self < other; },
				[](Model& self, Object* other) { return &self < other; }
			),
			sol::meta_function::less_than, sol::overload(
				[](Model& self, shared_ptr<Model> other) { return &self < other.get(); },
				[](Model& self, shared_ptr<Entity> other) { return &self < other.get(); },
				[](Model& self, shared_ptr<Object> other) { return &self < other.get(); }
			),
			sol::meta_function::less_than_or_equal_to, sol::overload(
				[](Model& self, Model* other) { return &self <= other; },
				[](Model& self, Entity* other) { return &self <= other; },
				[](Model& self, Object* other) { return &self <= other; }
			),
			sol::meta_function::less_than_or_equal_to, sol::overload(
				[](Model& self, shared_ptr<Model> other) { return &self <= other.get(); },
				[](Model& self, shared_ptr<Entity> other) { return &self <= other.get(); },
				[](Model& self, shared_ptr<Object> other) { return &self <= other.get(); }
			),

Any advice on how to get the table compare to recognize that two different shared pointers to the same object are the same?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions