流插入运算符的关联性是rtl,忘记这个事实有时会导致运行时或逻辑错误.例如:
一号通
int F()
{
static int internal_counter c=0;
return ++c;
}
Run Code Online (Sandbox Code Playgroud)
在主要功能:
//....here is main()
cout<<”1st=”<<F()<<”,2nd=”<<F()<<”,3rd=”<<F();
Run Code Online (Sandbox Code Playgroud)
输出是:
1st=3,2nd=2,3rd=1
Run Code Online (Sandbox Code Playgroud)
这与我们初看起来的不同.
第二 - 假设我们有一个像这样的堆栈数据结构的实现:
//
//... a Stack<DataType> class ……
//
Stack<int> st(10);
for(int i=1;i<11;i++)
st.push(i);
cout<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl;
Run Code Online (Sandbox Code Playgroud)
预期输出是这样的:
10
9
8
7
Run Code Online (Sandbox Code Playgroud)
但我们有:
7
8
9
10
Run Code Online (Sandbox Code Playgroud)
没有内部错误的<<实现,但它可能会令人困惑......最后[:-)]我的问题:有没有办法通过重载来改变运算符的关联性?
你觉得这可能不会逆转吗?我的意思是可以通过修改或更改开源STL来改变顺序吗?
小智 10
不,没有.但我认为您可能会将评估顺序与关联性混为一谈.指定evalualtion顺序的唯一运算符是&&,|| 和,(逗号).当你说:
cout<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl<<st.pop()<<endl;
Run Code Online (Sandbox Code Playgroud)
编译器可以按照自己喜欢的顺序计算st.pop()等子表达式,这是导致意外输出的原因.
唯一正确关联的东西是赋值运算符.见标准的§5.4至5.18.的<<运算符的计算左到右或消息将在语法是向后,而不是在内容.内容是由于副作用,在C++中无序,除了(如Neil提到的)"短路"&&和||以及逗号.