不要被sizeof(i++)、sizeof(++i)、sizeof(fun())给绊倒了------杜绝写这种易误导人的代码

        看程序:

#include 
using namespace std;

double fun()
{
	cout << "oh, my god" << endl;
	return 0.0;
}

int main()
{
	int i = 0;
	int a = sizeof(i++); // sizeof在编译期间计算, i++不执行
	cout << i << endl;
	cout << a << endl;

	a = sizeof(++i);
	cout << i << endl;	// sizeof在编译期间计算, ++i不执行
	cout << a << endl;

	a = sizeof(fun());  // sizeof在编译期间计算, fun函数不执行
	cout << a << endl;

	return 0;
}
      结果为:

0
4
0
4
8


      要杜绝写这种易误导人的代码。 实际项目中, 如果遇到这样的代码, 引入了bug, 定位起来, 会被坑死的啊。 




你可能感兴趣的:(S1:,C/C++)