-
类型差异
-
NULL和0#include <iostream> template <typename T> void show( T&& a) { a.error(); } int main () { show(NULL); }D:\codes\cfile\Test\test.cpp: In instantiation of 'void show(T&&) [with T = long long int]': D:\codes\cfile\Test\test.cpp:8:14: required from here D:\codes\cfile\Test\test.cpp:4:7: error: request for member 'error' in 'a', which is of non-class type 'long long int' a.error(); ~~^~~~~ [Finished in 328ms] -
问题
-
错误匹配
#include <iostream> void show(long long int) { std::cout << __LINE__ << std::endl; } void show(void *) { std::cout << __LINE__ << std::endl; } int main () { show(NULL); } -
二义性
#include <iostream> void show(bool) { std::cout << __LINE__ << std::endl; } void show(void *) { std::cout << __LINE__ << std::endl; } int main () { show(NULL); } -
小结
-
-
好处
-
模板
-
总结
c++11 推荐 nullptr 而不是 NULL
最新推荐文章于 2024-11-25 15:22:53 发布
本文探讨了C++中NULL与0作为内置类型在重载函数和模板中的行为差异,包括错误匹配、二义性和nullptr的优势。作者强调了nullptr的可读性、编译器支持及避免类型歧义,提倡使用nullptr而非NULL或0.
1010

被折叠的 条评论
为什么被折叠?



