C++ c++;11 lambda capturelist参考
下面是我关于c++11 lambda的练习代码:C++ c++;11 lambda capturelist参考,c++,c++11,lambda,cout,C++,C++11,Lambda,Cout,下面是我关于c++11 lambda的练习代码: #include<iostream> int d = 0; int main() { int e = 1; auto i = [&]() ->int { e += 1; d += 1; return d;}; d += 1; std::cout << "the value of d:" << d <<
#include<iostream>
int d = 0;
int main()
{
int e = 1;
auto i = [&]() ->int {
e += 1;
d += 1;
return d;};
d += 1;
std::cout << "the value of d:" << d << std::endl;
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << std::endl;
std::cout << " e:" << e << " d:" << d << std::endl;
return 0;
}
我只是不明白为什么
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << std::endl;
std::cout << " e:" << e << " d:" << d << std::endl;
std::cout函数参数的求值顺序未指定。所以函数参数可以从右向左或从左向右求值。
编译器似乎从右向左计算参数。所以在这个声明中
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << << std::endl;
考虑到在此语句中使用的运算符的操作数的求值顺序与std::cout的未定义行为相同:@Csq求值顺序确实未指定,但这不是未定义的行为。此程序中没有对同一对象的未排序修改。@Casey OP正在修改e
,并在写入(…)i()@Csqi()时以相同的顺序点打印它
是一个函数调用:i
中的修改因此在调用i
之前和i
返回之后发生的所有事情之后排序。表达式i()
和e
的求值顺序未指定,但并非未排序。
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << << std::endl;
the value of i():2
e:1 d:1
int a = 10, b = 20;
std::cout << a << ' ' << b;
int a = 10, b = 20;
std::operator <<( std::cout.operator <<( a ), ' ' ).operator <<( b );
std::cout << "the value of i():" << i() << std::endl
std::cout << " e:" << e << " d:" << d << << std::endl;