I try to find a way in C++17 to shorten following function calls:
GetCurrentContext()->GetEntityManager()->CreateEntity();
maybe to something like:
EM()->CreateEntity();
I tried a function pointer, but this is only valid for static functions:
constexpr auto &EM = GetContext()->GetEntityManager;
// error: reference to non-static member function must be called;
Is there a better solution than using a Macro?
#define EM GetContext()->GetEntityManager
When using an inline function call, i'm afraid, that the compiler will ignore this and i have an unnessessary overhead:
inline EntityManager* EM() { return GetCurrentContext->GetEntityManager(); }
Also this seems to be the wrong way to go, because i'm looking for an alias, not for another function to define.
EDIT:
Every Context has an EntityManager and the current Context can change during runtime. So i'm really looking for an alias, not for a const pointer to what the function returns.
UPDATE:
I found this Question. With return type auto, the inline function becomes independet from the original return type. Even if the original function will get changed in future, the alias need not be touched again. And with trust in compiler optimization this will really become a real alias.
So i think (In consideration of the answers and comments) the best solution is to do the following:
inline decltype(auto) EM() { return GetCurrentContext()->GetEntityManager(); }
decltype(auto)
instead ofauto
. IfGetEntityManager()
ever changes to return a reference (maybe a reference to smart pointer??),decltype(auto)
will preserve the reference type, but justauto
will not. – Collator