Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

Summary

This PR implements a new MethodDelegate class that addresses issue #27 by providing automatic type deduction from member function pointers, enabling a more convenient syntax for creating delegates.

Key Features

Automatic Type Deduction: Uses member function pointer as template parameter to automatically deduce class type, return type, and argument types

Convenient Syntax: Instead of:

Delegate<void(const char *)> memberMethod(std::make_shared<Object>(), &Object::Method);

You can now write:

MethodDelegate<&Object::Method> memberMethod(std::make_shared<Object>());

Backward Compatibility: Existing Delegate<ReturnType(Args...)> syntax continues to work unchanged

Seamless Conversion: MethodDelegate can be implicitly converted to traditional Delegate when needed

Implementation Details

  • MemberFunctionTraits: Template helper that extracts type information from member function pointers
  • MethodDelegate<method>: New class template that takes a member function pointer as a non-type template parameter
  • Perfect Forwarding: Supports universal references for optimal argument forwarding
  • Type Safety: Compile-time type checking ensures correct usage

Example Usage

struct Object {
    void Method(const char *str) {
        std::cout << "Object::Method(" << str << ")" << std::endl;
    }
};

// New convenient syntax
auto obj = std::make_shared<Object>();
MethodDelegate<&Object::Method> delegate(obj);
delegate("Hello World");

// Can be converted to traditional delegate
Delegate<void(const char*)> traditional = delegate;

// Works with generic functions
void call(auto&& delegate) {
    delegate("calling delegate");
}
call(delegate);

Test Coverage

The implementation includes comprehensive tests covering:

  • Basic functionality with different argument types
  • Return value handling
  • Multiple arguments
  • String parameters
  • Equality comparison
  • Conversion to traditional delegates
  • Generic function compatibility

Test plan

  • All existing tests pass (backward compatibility maintained)
  • New MethodDelegate tests pass
  • Demo program runs successfully
  • Type deduction works correctly for various method signatures
  • Conversion between MethodDelegate and traditional Delegate works
  • Perfect forwarding preserves argument types

🤖 Generated with Claude Code


Resolves #27

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

Issue: #27
@konard konard self-assigned this Sep 13, 2025
…nters

- Add MemberFunctionTraits helper for extracting types from member function pointers
- Implement MethodDelegate<method> class that takes method pointer as template parameter
- Support automatic conversion to traditional Delegate<ReturnType(Args...)> format
- Add comprehensive tests covering various scenarios (basic usage, return values, multiple args, equality, conversion)
- Maintain backward compatibility with existing Delegate implementation
- Add demo showing the new syntax: MethodDelegate<&Class::method> instead of Delegate<void(args...)>

This addresses issue #27 by providing a more convenient syntax for creating delegates from member functions.

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

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] A better delegate type definition? Implement MethodDelegate for automatic type deduction from method pointers Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 01:48
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.

A better delegate type definition?

2 participants