NULL
cppreference.net에서[에서
| 다음 헤더에 정의됨 <clocale>
|
||
| 다음 헤더에 정의됨 <cstddef>
|
||
| 다음 헤더에 정의됨 <cstdio>
|
||
| 다음 헤더에 정의됨 <cstdlib>
|
||
| 다음 헤더에 정의됨 <cstring>
|
||
| 다음 헤더에 정의됨 <ctime>
|
||
| 다음 헤더에 정의됨 <cwchar>
|
||
#define NULL /* implementation-defined */
|
||
매크로 NULL 는 구현에서 정의하는 널 포인터 상수입니다.
가능한 구현
#define NULL 0 // since C++11 #define NULL nullptr |
참고 사항
C 언어에서는 매크로
NULL
이
void
*
타입을 가질 수 있지만, C++에서는 널 포인터 상수가 해당 타입을 가질 수 없으므로 허용되지 않습니다.
예제
이 코드 실행
#include <cstddef> #include <iostream> #include <type_traits> #include <typeinfo> class S; int main() { int* p = NULL; int* p2 = static_cast<std::nullptr_t>(NULL); void(*f)(int) = NULL; int S::*mp = NULL; void(S::*mfp)(int) = NULL; auto nullvar = NULL; // gcc/clang으로 컴파일 시 경고를 발생시킬 수 있음 std::cout << "The type of nullvar is " << typeid(nullvar).name() << '\n'; if constexpr(std::is_same_v<decltype(NULL), std::nullptr_t>) std::cout << "NULL implemented with type std::nullptr_t\n"; else std::cout << "NULL implemented using an integral type\n"; [](...){}(p, p2, f, mp, mfp); // < "사용되지 않은 변수" 경고를 억제함 }
가능한 출력:
The type of nullvar is long NULL implemented using an integral type
참고 항목
| nullptr (C++11) | 널 포인터 값을 지정하는 포인터 리터럴 |
|
(C++11)
|
널 포인터 리터럴의 타입
nullptr
(typedef) |
|
C 문서
for
NULL
|
|