什么时候计算参数,当连接调用时:obj.F1()。F2()。F3(sin(x))? [英] When are parameters calculated, when having concatenated call: obj.F1().F2().F3( sin(x) )?

查看:157
本文介绍了什么时候计算参数,当连接调用时:obj.F1()。F2()。F3(sin(x))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用流操作符(例如运算符<<(const char *))进行记录。在我的单元测试中,我有如下测试:

  MyLogger oLogger; 
oLogger<< 查理;
oLogger<< foo<<睡眠(10)<< 酒吧;
oLogger<< 马库斯;

首先我执行第二个线程,它应该同时登录。我想知道,为什么在foobar之间没有生成其他的日志输出。所以我在当前时间在每个操作员身上打印。我预计如此:

  50秒137051 usec charly 
50 sec 137930 usec foo
60 sec 138014 usec 0
60 sec 138047 usec bar
60 sec 138088 usec marcus

但得到了这个:
$ b $ pre $ 50 $ 137051 usec charly
60 sec 137930 usec foo
60 sec 138014 usec 0
60秒138047 usec bar
60 sec 138088 usec marcus

什么是错误的想法如下:

  [0秒] MyLogger&处理MyLogger :: operator<(charly)。 
[0秒] MyLogger&处理MyLogger :: operator<(foo)。
返回值用于下一个函数。
[0秒]处理int sleep(10) - 这需要10秒钟,直到返回的
值可用
[10秒] MyLogger& MyLogger :: operator <=(0)被处理
(0是sleep(10)的返回值)
[10秒] MyLogger& MyLogger :: operator<(bar)被处理

但是由于什么原因,在 sleep(10)

之后处理 MyLogger ::运算符<(foo)

解决方案

这是可接受的行为,因为未指定操作符以operator<正在被评估,并且TTBOMK gcc经常以相反的顺序进行操作,以便按照正确的顺序将参数传递到堆栈上,以便进行下一个函数调用。





  pushcharly
push oLogger
调用操作符<<
pop
pushbar
push 10
call sleep
pushfoo
push oLogger
call operator<<
呼叫运营商<<
呼叫运营商<<
pop


I use the streaming operators (e.g. operator<<(const char*)) for logging. In my unit tests I have a test like the following:

MyLogger oLogger;
oLogger << "charly";
oLogger << "foo" << sleep( 10 ) << "bar";
oLogger << "marcus";

First I executed a second thread, which should log at the same time. I was wondering, why between "foo" and "bar" no other log-output was generated. So I printed in each operator the current time . I expected something like this:

50 sec 137051 usec charly 
50 sec 137930 usec foo 
60 sec 138014 usec 0 
60 sec 138047 usec bar 
60 sec 138088 usec marcus 

but got this:

50 sec 137051 usec charly 
60 sec 137930 usec foo 
60 sec 138014 usec 0 
60 sec 138047 usec bar 
60 sec 138088 usec marcus 

What is wrong with the following idea:

[ 0 sec] MyLogger& MyLogger::operator<<( "charly" ) is processed.
[ 0 sec] MyLogger& MyLogger::operator<<( "foo" ) is processed.
         The return value is used for the next function.
[ 0 sec] int sleep( 10 ) is processed - this takes 10 seconds until the return
         value is available
[10 sec] MyLogger& MyLogger::operator<<( 0 ) is processed
         ( "0" is the return value of sleep(10) )
[10 sec] MyLogger& MyLogger::operator<<( "bar" ) is processed

But for what reason ever, the MyLogger::operator<<( "foo" ) is processed after sleep(10)?

解决方案

This is acceptable behaviour, as it is not specified in which order operands to "operator<<" are being evaluated, and TTBOMK gcc often does it in reverse order to get the parameters onto the stack in the right order for the next function call.

Think of it in terms of a stack machine:

push "charly"
push oLogger
call operator<<
pop
push "bar"
push 10
call sleep
push "foo"
push oLogger
call operator<<
call operator<<
call operator<<
pop

这篇关于什么时候计算参数,当连接调用时:obj.F1()。F2()。F3(sin(x))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆