-1

How to alias a certain template function to make it called by using shorter syntax?

From ....

getSystem<SystemX>()->    //get semi-singleton instance of "SystemX"

... to something like this:-

getSystem<SystemX>->       or
getSystem(SystemX)->       or
{SystemX}->                or
SystemX=>                 (can I define new strange operator?)   

At first, I don't think it is a problem at all, but after several months, I think there might be a more concise way (syntax) to call it. (I use it in 400+ location).

I believe it is possible by using the same trick as std::is_enable_t<T>=std::is_enable<T>::value. (?)

full code

My poor workarounds

Besides making the name shorter e.g. getSystem<SystemA>()-> to ss<A>()-> , here are my workarounds.

Solution A

Instead of calling getSystem<SystemA>()->, I would call SystemA:: instead.

Disadvantage:

  • Every function of system now become static function.

  • There can't be any 2 instance of the same system inside my program anymore.

  • Common disadvantage of singleton : Global variable, break single responsibility, etc.

Solution B

By using macro, the result is exactly what I want :-

#define S(param) getSystem<param>()   
S(SystemA)->fa()     

However, macro has some disadvantages.
I feel that this is not a place to use this type of hack.

Sorry, if it is too newbie, I am very new to C++.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
cppBeginner
  • 1,114
  • 9
  • 27
  • So `getSystem()` is defined in the base class? Does it return a static object? What does it do? Typically when I need a child object to communicate with a parent, I use a reference or pointer of some kind. – Dustin Nieffenegger Jun 07 '17 at 05:26
  • @Dustin Goodson Yes, it is defined in base class like `templateT* getSystem(){...}`. It returns non-static object i.e. instance of `new SystemA()`. Such instances were injected by another top-level class since start of the game (it is a bit off-topic, so I will stop here). – cppBeginner Jun 07 '17 at 05:32
  • What is so wrong in `getSystem()` vs `getSystem(SystemX)` does the pair of empty parentheses hurt so much? You could use template argument deduction but I doubt it would improve syntax. – luk32 Jun 07 '17 at 11:20
  • @luk32 At first, I don't think it is, but after typing it several times, I believe I could be a little more productive and code would be more readable if there is an alias (just for this function). Sorry if it sounds too newbie. – cppBeginner Jun 09 '17 at 01:35
  • I don't see anything wrong with typing getSystem(), unless you have 100s of such calls for only a few SystemX classes (which may indicate some logic issues in your function). You could use aliases at the cpp. level to save some key strokes, something like using A=SystemA; etc. Then you'd have something like getSystem(), etc... – Michaël Roy Jun 15 '17 at 19:34
  • @Michaël Roy Thank for a workaround. It would be handy in such highly-repetitive cases. :) – cppBeginner Jun 16 '17 at 02:11

1 Answers1

0

How about:

class SystemB : public SystemBase {
    inline auto getSystemA(void) { return getSystem<SystemA>(); }
    inline auto getSystemB(void) { return getSystem<SystemB>(); }
    void fb();
};
void SystemB::fb(){
    getSystemA()->fa();
}

Since you want to use the SystemBase::getSystem from the SystemB object, the function goes in the SystemB class.

TrentP
  • 4,240
  • 24
  • 35