Reputation: 405
I have following delegate class:
template <typename RetVal, typename ...Args>
class KxCEventDelegate
{
union InstancePtr
{
InstancePtr(void) : as_void(nullptr) {}
void* as_void;
const void* as_const_void;
};
typedef RetVal(*InternalFunction)(InstancePtr, Args&& ...args);
typedef std::pair<InstancePtr, InternalFunction> Stub;
// Turns a free function into internal function stub
template <RetVal(*Function)(Args ...args)>
static KX_INLINE RetVal FunctionStub(InstancePtr, Args&& ...args)
{
// we don't need the instance pointer because we're dealing with free functions
return (Function)(std::forward<Args>(args)...);
}
// Turns a member function into internal function stub
template <class C, RetVal (C::*Function)(Args ...args)>
static KX_INLINE RetVal ClassMethodStub(InstancePtr instance, Args&& ...args)
{
// cast the instance pointer back into the original class instance
return (static_cast<C*>(instance.as_void)->*Function)(std::forward<Args>(args)...);
}
// Turns a member function into internal function stub
template <class C, RetVal(C::*Function)(Args ...args) const>
static KX_INLINE RetVal ClassMethodStubConst(InstancePtr instance, Args&& ...args)
{
// cast the instance pointer back into the original class instance
return (static_cast<const C*>(instance.as_const_void)->*Function)(std::forward<Args>(args)...);
}
public:
// Binds a free function
template <RetVal(*Function)(Args ...args)>
void Bind(void)
{
m_stub.first.as_void = nullptr;
m_stub.second = &FunctionStub<Function>;
}
// Binds a class method
template <class C, RetVal (C::*Function)(Args ...args)>
void Bind(C* instance)
{
m_stub.first.as_void = instance;
m_stub.second = &ClassMethodStub<C, Function>;
}
// Binds a class method
template <class C, RetVal(C::*Function)(Args ...args) const>
void BindConst(const C* instance)
{
m_stub.first.as_const_void = instance;
m_stub.second = &ClassMethodStubConst<C, Function>;
}
// Invokes the delegate
RetVal Invoke(Args ...args) const
{
KX_ASSERT(m_stub.second != nullptr, "Cannot invoke unbound delegate. Call Bind() first.", m_stub.second, nullptr);
return m_stub.second(m_stub.first, std::forward<Args>(args)...);
}
private:
Stub m_stub;
};
Usage is like this (for free functions):
int FreeFunctionInt(int i)
{
return i;
}
KxCEventDelegate<int, int> delegate;
delegate.Bind<&FreeFunctionInt>();
int ret = delegate.Invoke(10);
Now, I am trying to implement a generic GetEventDelegate function, similar to c#. This is what I came with:
// Gets event delegate.
template <typename RetVal, typename ...Args>
KxCEventDelegate<RetVal, Args...> GetEventDelegate(RetVal(*Function)(Args ...args))
{
KxCEventDelegate<RetVal, Args...> delegate;
delegate.Bind<Function>();
return delegate;
}
This is the part that I cant figure out what is wrong. It seems to be a problem with delegate.Bind<Function>();
. Compiler gives me following errors:
1>------ Build started: Project: Tests, Configuration: Debug x64 ------
1>Main.cpp
1>c:\sdk\kx\kxengine\include\events\delegate.h(88): error C2672: 'kx::events::KxCEventDelegate<int,int>::Bind': no matching overloaded function found
1>c:\sdk\kx\tests\eventstests.h(76): note: see reference to function template instantiation 'kx::events::KxCEventDelegate<int,int> kx::events::GetEventDelegate<int,int>(RetVal (__cdecl *)(int))' being compiled
1> with
1> [
1> RetVal=int
1> ]
1>c:\sdk\kx\kxengine\include\events\delegate.h(88): error C2974: 'kx::events::KxCEventDelegate<int,int>::Bind': invalid template argument for 'C', type expected
1>c:\sdk\kx\kxengine\include\events\delegate.h(58): note: see declaration of 'kx::events::KxCEventDelegate<int,int>::Bind'
1>c:\sdk\kx\kxengine\include\events\delegate.h(88): error C2975: 'Function': invalid template argument for 'kx::events::KxCEventDelegate<int,int>::Bind', expected compile-time constant expression
1>c:\sdk\kx\kxengine\include\events\delegate.h(49): note: see declaration of 'Function'
1>Done building project "Tests.vcxproj" -- FAILED.
Upvotes: 1
Views: 2169
Reputation: 50550
In this snippet, your generic GetEventDelegate
function:
template <typename RetVal, typename ...Args>
KxCEventDelegate<RetVal, Args...> GetEventDelegate(RetVal(*Function)(Args ...args)) {
KxCEventDelegate<RetVal, Args...> delegate;
delegate.Bind<Function>();
return delegate;
}
Function
is not a constant expression as it ought to be if you intend to use it as a template argument.
In C++11/14, you can work around it with another level of indirection. Something like this:
template <typename RetVal, typename ...Args>
struct Factory {
template<RetVal(*Function)(Args...)>
static KxCEventDelegate<RetVal, Args...> GetEventDelegate() {
KxCEventDelegate<RetVal, Args...> delegate;
delegate.Bind<Function>();
return delegate;
}
};
That you can use as it follows:
auto delegate = Factory<int, int>::GetEventDelegate<&FreeFunctionInt>();
Anyway I'd suggest to add a static function to the delegate class instead and use it as a factory method directly embedded in the type itself.
Something you'll end up invoking as:
auto delegate = KxCEventDelegate<int, int>::create<&FreeFunctionInt>();
It's easier for a reader to understand what's going on under the hood, at least from my point of view.
Upvotes: 2