Rin*_*o_D 5 c++ templates objective-c type-traits
AFAIK,开发人员可以使用 C++ 类型特征来了解类的“类型”和类之间的关系。其中一些是由编译器实现的。
例如,std::is_base_of<b, d>::value如果b是 的基类,则返回 true d。它由__is_base_of.
而且有一些技巧在这里。
现在我试图找到 ObjC 类的规则。
至于测试代码:
@interface Super : NSObject
@end
@implementation Super
@end
@interface SubA : Super
@end
@implementation SubA
@end
@interface SubB : Super
@end
@implementation SubB
@end
int main()
{
NSLog(@"SubA is base of Super %d", std::is_base_of<SubA, Super>::value);
NSLog(@"SubA* is base of Super* %d", std::is_base_of<SubA*, Super*>::value);
NSLog(@"Super is base of SubA %d", std::is_base_of<Super, SubA>::value);
NSLog(@"Super* is base of SubA* %d", std::is_base_of<Super*, SubA*>::value);
NSLog(@"SubA is convertible of Super %d", std::is_convertible<SubA, Super>::value);
NSLog(@"SubA* is convertible of Super* %d", std::is_convertible<SubA*, Super*>::value);
NSLog(@"Super is convertible of SubA %d", std::is_convertible<Super, SubA>::value);
NSLog(@"Super* is convertible of SubA* %d", std::is_convertible<Super*, SubA*>::value);
NSLog(@"SubA is convertible of SubB %d", std::is_convertible<SubA, SubB>::value);
NSLog(@"SubA* is convertible of SubB* %d", std::is_convertible<SubA*, SubB*>::value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
SubA is base of Super 0
SubA* is base of Super* 0
Super is base of SubA 0
Super* is base of SubA* 0
SubA is convertible of Super 0
SubA* is convertible of Super* 1
Super is convertible of SubA 0
Super* is convertible of SubA* 1
SubA is convertible of SubB 0
SubA* is convertible of SubB* 0
Run Code Online (Sandbox Code Playgroud)
这个输出超出了我的预期。由于所有 ObjC 类都是 C struct 的基础objc_class,我认为std::is_base_ofObjcC 类之间的所有类都应该是假的,并且都std::is_convertible应该是真的。
那么里面的原理是什么呢?对 objc 类使用 C++ 类型特征时应该遵循哪些规则?