那年C++98那些奇怪的特性?详见文章!
2025-10-30 17:01:25
发布于:广东
有一段C++代码:
#include<iostream>
using namespace std;
func(int a){
cout<<a<<endl;
return a;
}
main(){
cout<<func(12);
return 0;
}
乍一看:这玩意明显不符合C++规范啊!但是——
- 打开NOI Linux虚拟机
- 终端打开,C++98编译:
g++ ~/file.cpp -o file -std=c++98 && file
一看终端——
12
12
为什么?为什么还能正常运行!
但是换一个C++11:g++ ~/file.cpp -o file -std=c++11 && file
报错不这就来了吗?
main.cpp:3:11: error: ISO C++ forbids declaration of ‘func’ with no type [-fpermissive]
3 | func(int a){
| ^
main.cpp:7:6: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
7 | main(){
| ^
这个特性是C98独有的,定义函数时如果不声明类型会自动变成int。但是为了规范代码,这个特效在C11被废除了。
各位狗友们,写代码时一定要规范,不然就会变成sh*t山代码呢!容易被GNU G++制裁哦!
这里空空如也







有帮助,赞一个