我正在编写c表达式解析器并发现我不理解的行为:
#include <iostream>
#include <sstream>
int main()
{
std::string string1;
std::string string2 = std::string((string1 = std::string("first")) + " " + (string1 = std::string("second")));
std::cout << string1 << std::endl;
std::cout << string2 << std::endl;
int int1;
int int2 = (int1 = 1) + (int1 = 2);
std::cout << int1 << std::endl;
std::cout << int2 << std::endl;
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
first
first first
2
4
Run Code Online (Sandbox Code Playgroud)
我曾预料到:
second
first second
2
3
Run Code Online (Sandbox Code Playgroud)
在C#中运行相同的程序时,我得到了预期的输出.你能解释一下那里发生了什么吗?
C#代码:https://gist.github.com/Kukkimonsuta/59543cfc4f7f73b8bebd
此代码在C++中具有未定义的行为,因为单个表达式会对同一个变量产生多个副作用(即两个赋值string1).这同样适用于int1变量.
在您的情况下,编译器将按照与您预期相反的顺序应用这两个副作用.即使它按照您预期的顺序应用它们,该程序仍然无效.
在C#中运行相同的程序时,我得到了预期的输出.
与C++不同,C#并没有给编译器开发人员留下太多的决定.这些表达式的评估顺序以及在C++中产生未定义行为的许多其他表达式都由语言标准很好地定义.
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |