好的我需要扩展代码,但这里有一些非常复杂的东西:
((Traces.Last=new SymbolSequence::Node)->Prev=(Discard.First=Processing1)->Prev)->Next=Traces.Last;
我需要将这些赋值分配给语句,但我无法确定这个的顺序。 括号中的顺序让我感到困惑,是数学还是编译器做他想做的事情?
非常感谢。
答案 0 :(得分:8)
代码可能意图是
Traces.Last = new SymbolSequence::Node;
Discard.First = Processing1;
Traces.Last->Prev = Discard.First->Prev;
Traces.Last->Prev->Next = Traces.Last;
但是,由于它都访问Traces.Last并设置它而没有插入序列点,因此实际结果是未定义的。编译器很可能产生类似的东西:
temp = Traces.Last;
Traces.Last = new SymbolSequence::Node;
Discard.First = Processing1;
Traces.Last->Prev = Discard.First->Prev;
Traces.Last->Prev->Next = temp;
并且原始程序员想要类似的东西。
答案 1 :(得分:2)
我理解你的痛苦。开发人员的代码就好像整个世界都依赖于一行代码,应该在他们的女朋友面前用湿面条拍打。
当重构这样的代码时,一次启动一个部分总是有用的。并随时测试。
取一个位于最里面的括号组中的小表达式并重构出来。也许通过将其分配给变量。每个代码都不同,所以你尽你所能。这将逐渐缩小大量代码。 继续这样做,直到你的表达越来越小。它还有助于在运算符之间放置空格,以便实际查看代码的分组并帮助收集评估顺序。
所以给出你的原始代码:
((Traces.Last=new SymbolSequence::Node)->Prev=(Discard.First=Processing1)->Prev)->Next=Traces.Last;
将其拆分为:
Traces.Last = new SymbolSequence::Node
((Traces.Last)->Prev = (Discard.First = Processing1)->Prev)->Next = Traces.Last;
现在处理另一项内部任务:
Traces.Last = new SymbolSequence::Node
Discard.First = Processing1
((Traces.Last)->Prev = (Discard.First)->Prev)->Next = Traces.Last;
然后我处理下一个最里面的任务
Traces.Last = new SymbolSequence::Node
Discard.First = Processing1
(Traces.Last)->Prev = (Discard.First)->Prev
( (Traces.Last)->Prev )->Next = Traces.Last;
我希望有所帮助。
答案 2 :(得分:0)
除了评论中描述的未定义行为之外,下面是根据clang++
的AST表示。 AST显示运算符在解析时的优先级。
我必须使用某些类型来引导您的示例以满足clang++
:
struct SymbolSequence
{
struct Node
{
Node *First;
Node *Last;
Node *Prev;
Node *Next;
};
};
void foo()
{
SymbolSequence::Node *Processing1;
SymbolSequence::Node Traces;
SymbolSequence::Node Discard;
((Traces.Last=new SymbolSequence::Node)->Prev=(Discard.First=Processing1)->Prev)->Next=Traces.Last;
}
AST:
void foo() (CompoundStmt 0x308b6e0 <go.cpp:14:1, line:19:1>
(DeclStmt 0x3088dc8 <line:15:5, col:38>
(0x3088d70 "SymbolSequence::Node *Processing1"))
(DeclStmt 0x30891d8 <line:16:5, col:32>
(0x3088e40 "SymbolSequence::Node Traces =
(CXXConstructExpr 0x30891a8 <col:26> 'struct SymbolSequence::Node':'struct SymbolSequence::Node''void (void)')"))
(DeclStmt 0x30892d8 <line:17:5, col:33>
(0x3089250 "SymbolSequence::Node Discard =
(CXXConstructExpr 0x30892a8 <col:26> 'struct SymbolSequence::Node':'struct SymbolSequence::Node''void (void)')"))
(BinaryOperator 0x308b6b8 <line:18:5, col:99> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x308b618 <col:5, col:87> 'struct SymbolSequence::Node *' lvalue ->Next 0x3088b60
(ImplicitCastExpr 0x308b600 <col:5, col:84> 'struct SymbolSequence::Node *' <LValueToRValue>
(ParenExpr 0x308b5e0 <col:5, col:84> 'struct SymbolSequence::Node *' lvalue
(BinaryOperator 0x308b5b8 <col:6, col:80> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x308b448 <col:6, col:46> 'struct SymbolSequence::Node *' lvalue ->Prev 0x3088b00
(ImplicitCastExpr 0x308b430 <col:6, col:43> 'struct SymbolSequence::Node *' <LValueToRValue>
(ParenExpr 0x308b410 <col:6, col:43> 'struct SymbolSequence::Node *' lvalue
(BinaryOperator 0x308b3e8 <col:7, col:39> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x3089318 <col:7, col:14> 'struct SymbolSequence::Node *' lvalue .Last 0x3088aa0
(DeclRefExpr 0x30892f0 <col:7> 'struct SymbolSequence::Node':'struct SymbolSequence::Node' lvalue Var 0x3088e40 'Traces' 'struct SymbolSequence::Node':'struct SymbolSequence::Node'))
(CXXNewExpr 0x308b390 <col:19, col:39> 'struct SymbolSequence::Node *'
(CXXConstructExpr 0x308b360 <col:23> 'struct SymbolSequence::Node':'struct SymbolSequence::Node''void (void)'))))))
(ImplicitCastExpr 0x308b5a0 <col:51, col:80> 'struct SymbolSequence::Node *' <LValueToRValue>
(MemberExpr 0x308b570 <col:51, col:80> 'struct SymbolSequence::Node *' lvalue ->Prev 0x3088b00
(ImplicitCastExpr 0x308b558 <col:51, col:77> 'struct SymbolSequence::Node *' <LValueToRValue>
(ParenExpr 0x308b538 <col:51, col:77> 'struct SymbolSequence::Node *' lvalue
(BinaryOperator 0x308b510 <col:52, col:66> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x308b4a0 <col:52, col:60> 'struct SymbolSequence::Node *' lvalue .First 0x3088a40
(DeclRefExpr 0x308b478 <col:52> 'struct SymbolSequence::Node':'struct SymbolSequence::Node' lvalue Var 0x3089250 'Discard' 'struct SymbolSequence::Node':'struct SymbolSequence::Node'))
(ImplicitCastExpr 0x308b4f8 <col:66> 'struct SymbolSequence::Node *' <LValueToRValue>
(DeclRefExpr 0x308b4d0 <col:66> 'struct SymbolSequence::Node *' lvalue Var 0x3088d70 'Processing1' 'struct SymbolSequence::Node *')))))))))))
(ImplicitCastExpr 0x308b6a0 <col:92, col:99> 'struct SymbolSequence::Node *' <LValueToRValue>
(MemberExpr 0x308b670 <col:92, col:99> 'struct SymbolSequence::Node *' lvalue .Last 0x3088aa0
(DeclRefExpr 0x308b648 <col:92> 'struct SymbolSequence::Node':'struct SymbolSequence::Node' lvalue Var 0x3088e40 'Traces' 'struct SymbolSequence::Node':'struct SymbolSequence::Node')))))