是否有任何方法来实现自定义类型限定符(类似于const)?我想只允许对具有相同资格的函数进行函数调用,以获得正确的资格。有没有办法构建C++自定义限定符?
比方说,我将有:
void allowedFunction();
void disallowedFunction();
//Only allowed to call allowed functions.
void foo()
{
allowedFunction();
disallowedFunction(); //Cause compile time error
}
//Is allowed to call any function it wants.
void bar()
{
allowedFunction();
disallowedFunction(); //No error
}
我想这样做的原因是因为我想确保函数调用上的特定线程只能调用实时的安全保护功能。由于许多应用程序都需要硬实时安全线程,因此在编译时有一些检测锁的方式可以保证我们很难检测到运行时错误。
要为该语言添加新关键字,请不要有机会(除非您能说服委员会)。您可能可以使用宏。 –
我想你可能会对此感兴趣:[Metaclasses:关于生成C++的思考](https://herbsutter.com/2017/07/26/metaclasses-thoughts-on-generative-c/) –
也许你可以把特定头文件中的实时安全函数声明? – Oliv