Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

Summary

Fixes issue #20 where creating a Delegate with a derived class object and member function pointer would fail during template argument deduction.

Problem

The original constructor template required the object type and member function class type to match exactly:

template <typename Class>
Delegate(std::shared_ptr<Class> object, ReturnType(Class:: *member)(Args...))

This caused compilation errors when using derived class objects with inherited member functions:

struct base { void foo() { std::cout << "foo"; } };
struct derived : public base {};

auto object = std::make_shared<derived>();
auto delegate = Delegate(object, &derived::foo); // Failed - deduced conflicting types for Class

Solution

Modified the constructor to accept separate template parameters for the object type and member function class type:

template <typename ObjectClass, typename MemberClass>
Delegate(std::shared_ptr<ObjectClass> object, ReturnType(MemberClass:: *member)(Args...))
    : Delegate(std::make_shared<MemberMethod<MemberClass>>(std::static_pointer_cast<MemberClass>(object), member)) { }
  • Uses std::static_pointer_cast to safely convert the derived object to the base class
  • Updated the corresponding template deduction guide
  • Maintains backward compatibility with existing code

Test Results

Added comprehensive test cases that verify:

  • ✅ Derived objects with base class methods
  • ✅ Multi-level inheritance scenarios
  • ✅ Return value handling
  • ✅ Virtual method polymorphism
  • ✅ Base pointer to derived object scenarios

All tests pass, confirming both the fix and backward compatibility.

🤖 Generated with Claude Code


Resolves #20

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #20
@konard konard self-assigned this Sep 13, 2025
Resolves issue #20 where calling Delegate with a derived class object
and member function pointer would fail during template argument deduction.

Changes:
- Modified constructor to accept separate template parameters for object
  type (ObjectClass) and member function type (MemberClass)
- Uses static_pointer_cast to safely convert derived object to base class
- Updated corresponding template deduction guide
- Added comprehensive test cases to verify the fix

This allows creating delegates like:
  auto derived = std::make_shared<Derived>();
  auto delegate = Delegate(derived, &Derived::baseMethod);

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Call methods in derived class Fix: Support derived class objects in Delegate constructor (#20) Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Call methods in derived class

2 participants