如何使用较短的语法对某个模板函数进行别名以使其调用?
从....
Python 3.5.2 :: Anaconda custom (x86_64)
...这样的事情: -
getSystem<SystemX>()-> //get semi-singleton instance of "SystemX"
起初,我根本不认为这是一个问题,但几个月之后,我认为可能有更简洁的方法(语法)来调用它。 (我在400+位置使用它)。
我认为可以使用与getSystem<SystemX>-> or
getSystem(SystemX)-> or
{SystemX}-> or
SystemX=> (can I define new strange operator?)
= std::is_enable_t<T>
相同的技巧。 (?)
除了使名称更短,例如std::is_enable<T>::value
到getSystem<SystemA>()->
,这是我的解决方法。
我会打电话给ss<A>()->
,而不是拨打getSystem<SystemA>()->
。
缺点:
我的程序中不能有任何同一系统的2个实例。
Common disadvantage of singleton:全局变量,打破单一责任等。
通过使用宏,结果正是我想要的: -
SystemA::
然而,macro has some disadvantages 我觉得这不是一个使用这种黑客的地方。
很抱歉,如果它太新手了,我对C ++很新。
答案 0 :(得分:0)
怎么样:
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();
}
由于您要使用SystemBase::getSystem
对象中的SystemB
,该函数将进入SystemB类。