NULL和nullptr和nil和Nil還有NSNull

NULL和nullptr程序員

在Clang 6.0 的stddef.h文件中能夠找到NULL和nullptr的聲明:web

1swift

2數組

3安全

4函數

5lua

6spa

7指針

8code

9

10

11

12

13

14

15

16

#undef NULL

#ifdef __cplusplus

#  if !defined(__MINGW32__) && !defined(_MSC_VER)

#    define NULL __null

#  else

#    define NULL 0

#  endif

#else

#  define NULL ((void*)0)

#endif

#ifdef __cplusplus

#if defined(_MSC_EXTENSIONS) && defined(_NATIVE_NULLPTR_SUPPORTED)

namespace std { typedef decltype(nullptr) nullptr_t; }

using ::std::nullptr_t;

#endif

#endif

早在1972年,C語言誕生的初期,常數0帶有常數及空指針的雙重身分。 C使用preprocessor macro NULL表示空指針,讓NULL及0分別表明空指針及常數0。 NULL可被定義爲((void*)0)或是0。

C++並不採用C的規則,不容許將void*隱式轉換爲其餘類型的指針。爲了使代碼char* c = NULL;能經過編譯,NULL只能定義爲0。這樣的決定使得函數重載沒法區分代碼的語義:

1

2

void foo(char *);

void foo(int);

C++建議NULL應當定義爲0,因此foo(NULL);將會調用foo(int),這並非程序員想要的行爲,也違反了代碼的直觀性。0的歧義在此處形成困擾。

C++11引入了新的關鍵字來表明空指針常數:nullptr,將空指針和整數0的概念拆開。 nullptr的類型爲nullptr_t,能隱式轉換爲任何指針或是成員指針的類型,也能和它們進行相等或不等的比較。而nullptr不能隱式轉換爲整數,也不能和整數作比較。

爲了向下兼容,0仍可表明空指針常數。

1

2

3

4

5

char* pc = nullptr;     // OK

int * pi = nullptr;     // OK

int    i = nullptr;     // error

  

foo(pc);           // 呼叫foo(char *)

PS:__MINGW32__是MinGW編譯器的預約義宏。_MSC_VER是微軟C/C++編譯器——cl.exe 編譯代碼時預約義的一個宏。_MSC_VER的值表示cl的版本。須要針對cl特定版本編寫代碼時,也可使用該宏進行條件編譯。

nil和Nil

Objective-C

nil定義爲實例對象的空值(a null instance)

Nil定義爲類對象的空值(a null class)

nil和Nil在objc.h和MacTypes.h文件中均有等價的聲明:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#ifndef Nil

# if __has_feature(cxx_nullptr)

#   define Nil nullptr

# else

#   define Nil __DARWIN_NULL

# endif

#endif

#ifndef nil

# if __has_feature(cxx_nullptr)

#   define nil nullptr

# else

#   define nil __DARWIN_NULL

# endif

#endif

根據Clang 3.7 文檔對__has_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」,__has_feature(cxx_nullptr)是用來判斷是否支持C++11中的nullptr特性的。在Objective-C中nil和Nil都是__DARWIN_NULL宏定義。按住CMD鼠標點擊進入_types.h:

1

2

3

4

5

6

7

8

9

10

11

12

13

#ifdef __cplusplus

#ifdef __GNUG__

#define __DARWIN_NULL __null

#else /* ! __GNUG__ */

#ifdef __LP64__

#define __DARWIN_NULL (0L)

#else /* !__LP64__ */

#define __DARWIN_NULL 0

#endif /* __LP64__ */

#endif /* __GNUG__ */

#else /* ! __cplusplus */

#define __DARWIN_NULL ((void *)0)

#endif /* __cplusplus */

由於Objective-C不是C++代碼,因此倒數第二行#define __DARWIN_NULL ((void *)0)此時高亮,意味着最終nil和Nil本質都爲((void *)0)

PS:其實若是隻看Objective-C中的nil和Nil定義不用這麼麻煩的,只需查看Objective-C Runtime Reference中的」Constants->Null Values」便可。

Swift

Swift 1.2 目前只有nil而沒有Nil。爲了安全性Swift新增了Optional類型來做爲一個容器。比如一個箱子裏面可能裝有某種類型的對象,也多是空的(nil)。箱子也能夠嵌套,也能夠去掉,但這都基於安全的解析、綁定等。Swift 的nil和 Objective-C 中的nil並不同。在 Objective-C 中,nil是一個指向不存在對象的指針。在 Swift 中,nil不是指針——它是一個肯定的值,用來表示值缺失。任何類型的可選值均可以被設置爲nil,不僅是對象(object)類型。

PS:有關Swift中的Optional類型的更多信息能夠參考個人另外一篇博文:Optionals and Optional Chaining in Swift

PS:曾幾什麼時候,Swift的nil還不是字面量,而是NilType類型的惟一實例。但這一切都是歷史了。

NSNull

NSNull在NSNull.h中的定義:

1

2

3

@interface NSNull : NSObject <nscopying, nssecurecoding>

+ (NSNull *)null;

@end</nscopying, nssecurecoding>

NSNull是個單例,只有一個方法null,也用來表示空值。但它出如今一些nil沒法勝任的場景來替代nil來表示空值。好比NSArray和NSDictionary中nil表明數組或字典的末尾(即便nil不出如今末尾,也會將其切斷,nil後面的值會丟失),此時只能用NSNull對象來表示空值:

1

2

3

4

NSNull *nullValue = [NSNull null];

NSArray *arrayWithNull = @[nullValue];

NSLog(@"arrayWithNull: %@", arrayWithNull);

// Output: "arrayWithNull: (<null>)"</null>

雖然NSNull語義上等同於nil,但卻並不徹底等於nil:

1

2

3

4

5

6

7

8

9

10

11

id aValue = [arrayWithNull objectAtIndex:0];

if (aValue == nil) {

    NSLog(@"equals nil");

}

else if (aValue == [NSNull null]) {

    NSLog(@"equals NSNull instance");

    if ([aValue isEqual:nil]) {

        NSLog(@"isEqual:nil");

    }

}

// Output: "equals NSNull instance"

相關文章
相關標籤/搜索