Garmaine Staff asked 2 years ago

How can I get and use the type of the return type of T::ID() for usage in my template args?

Must be done in C++11.

I have a template signature of:


template<typename EventType, typename IDType, std::unordered_map<IDType, uint64_t> ObjectRegistry::* MapRef >
uint64_t acquireHandleFromObject(EventType& id)
{
}

This maps an EventType to an unordered_map member on the ObjectRegistry class and performs some operations with it.

All objects of EventType may not have the same base but will have the method ID on them which returns a T ID class.

I am assuming that it should be possible for the template args to be reduced and infer this information from EventType using the following:


template<typename EventType, std::unordered_map<decltype(std::declval<EventType>().ID()), uint64_t> ObjectRegistry::* MapRef >
uint64_t acquireHandleFromObject(EventType& id)
{
}

But it does not work.

In my cpp when I try to specialize the new template signature intellisense reports that the specialization does not come back with a match:

template uint64_t ObjectRegistry::acquireHandleFromObject<NAMESPACE::SomeEvent, &ObjectRegistry::m_someMap>(NAMESPACE::SomeEvent& event);

Edit:

Nevermind I'm an idiot.

T::ID() returns a reference to an ID class. I needed to use std::decay::type on the decltype.