工程架构

__has_feature详解

2018-06-14  本文已影响63人  Miridescent

在iOS开发中总能看见__has_feature宏,最常见的例如__has_feature(objc_arc),表示支持自动引用计数机制(ARC),类似的还有objc_arc_weakobjc_arc_fields
__has_feature在Clang文档中的定义是这样的

__has_feature and __has_extension
These function-like macros take a single identifier argument that is the name of a feature. __has_feature evaluates to 1 if the feature is both supported by Clang and standardized in the current language standard or 0 if not (but see below), while __has_extension evaluates to 1 if the feature is supported by Clang in the current language (either as a language extension or a standard language feature) or 0 if not. They can be used like this:

大致的意思是通过给定的值,判断编译器是否支持该特性
类似的特性检测宏还有__has_builtin__has_attribute等,他们都属于Feature Checking Macros
类似的还有Include File Checking Macros大类的宏定义,用于检测是否包含文件,常见的有 __has_include

Clang Language Extensions文档连接

__has_feature支持哪些参数,可以看一下Clang源码PPMacroExpansion.cpp文件中的HasFeature方法,在文件的855行到1005行,方法再结合文档,基本可以熟练的使用这个宏了

PPMacroExpansion.cpp文件代码连接

上一篇下一篇

猜你喜欢

热点阅读