Skip to main content

C++ of the Day #2 - Sequence points.

Question

역시 C++ 뉴스그룹에 올라온 질문 하나 ((comp.lang.c++.moderated:Sequence points.))
int c = 0;
cout << c++ << c;
질문을 요약하면...
  1. 위 문장의 결과는 undefined라고 한다.
  2. 하지만 이 경우는 다음과 같이 번역되지 않는가?
    cout.operator<<(c++).operator<<(c);
    
  3. 인자를 evaluation 한 후에 함수가 호출되므로 이 사이에는 반드시 [[http://en.wikipedia.org/wiki/Sequence_point|sequence point]]가 있어야 하지 않나?
  4. 따라서 출력은 항상 01 이 되어야 하지 않나?


Answer

함수가 리턴하고 그 리턴된 객체의 함수가 호출되는 사이에는 sequence point가 존재합니다. 하지만 이는 함수 호출 사이의 순서를 보장해줄 뿐입니다. 위의 문장에서 두 인자의 evaluation 순서는 보장되지 않습니다. 설명을 쉽게 하기 위해 위의 문장을 다음 네개로 나누어보겠습니다.
  1. cout.operator<<
  2. c++
  3. *((1번의 operator<<가 리턴한 객체)).operator<<
  4. c
여기서 함수 사이의 sequence point에 의해 1번이 3번보다 먼저 호출된다는 것이 보장됩니다. 하지만 인자의 evaluation인 2번, 4번의 순서는 보장되지 않습니다. 위에서 질문자는 이 경우 2 -> 1 -> 4 -> 3번순으로 실행된다고 예상했으나 실제로는 4 -> 2 -> 1 -> 3번순으로 실행되어도 컴파일러로서는 표준을 위반하지 않은 것이 됩니다. 물론 이런 순서로 실행되면 출력은 00 이 됩니다. 여기서 중요한 점은 이런 순서의 차이점은 단지 출력만을 얘기하는 것이 아니라 인자가 evaluation되면서 생기는 모든 side effect에 대한 것이라는 점입니다. 간단한 해결책은 다음과 같이 두줄로 풀어쓰는 것입니다.
int c = 0;
cout << c++;
cout << c;
이 경우엔 문장의 끝(;)이 sequence point가 됩니다.

Comments

Popular posts from this blog

CodeHighlighter plugin test page.

This post is for testing CodeHighlighter plugin which uses GeSHi as a fontifier engine. ((Those code blocks are acquired from Google Code Search .)) ((For more supported languages, go CodeHighlighter plugin or GeSHi homepage.)) C++ (<pre lang="cpp" lineno="1">) class nsScannerBufferList { public: /** * Buffer objects are directly followed by a data segment. The start * of the data segment is determined by increment the |this| pointer * by 1 unit. */ class Buffer : public PRCList { public: Buffer() { ++index_; } PHP (<pre lang="php" lineno="4">) for ($i = 0; $i $value = ord( $utf8_string[ $i ] ); if ( $value < 128 ) { // ASCII $unicode .= chr($value); } else { if ( count( $values ) == 0 ) { $num_octets = ( $value } $values[] = $value; Lisp (<pre lang="lisp">) ;;; Assignment (define-caller-pattern setq ((:star var fo...

Textiler plugin test page

This post is for testing Textiler plugin . This plugin uses Textile engine (version 2.0.0). The sample text is come from Textile test page. (Note that the result will be vary according to your CSS options.) Supported wiki syntax Rendering result h2{color:green}. This is a title h3. This is a subhead p{color:red}. This is some text of dubious character. Isn't the use of "quotes" just lazy writing -- and theft of 'intellectual property' besides? I think the time has come to see a block quote. bq[fr]. This is a block quote. I'll admit it's not the most exciting block quote ever devised. Simple list: #{color:blue} one # two # three Multi-level list: # one ## aye ## bee ## see # two ## x ## y # three Mixed list: * Point one * Point two ## Step 1 ## Step 2 ## Step 3 * Point three ** Sub point 1 ** Sub point 2 Well, that went well. How about we insert an <a href="/" title="watch out">old-fashioned hypertext link</a>? Will the quo...

BookCover plugin test page

This post is for testing BookCover plugin . (( The previous test page has some comments from users.)) The 'Books Reading' section in the sidebar is also using this plugin. [ bookcover:1932394613] [bookcover:1932394613] [ bookcover:1590598385(Smart and Gets Things Done)] [bookcover:1590598385(Smart and Gets Things Done)] [ bookcover:1591841380(Wikinomics : How Mass Collaboration Changes Everything)] [bookcover:1591841380(Wikinomics : How Mass Collaboration Changes Everything)] [ bookcover:8995856408] [bookcover:8995856408] [ bookcover:8831793721] [bookcover:8831793721] [ bookcover:9788831793728] [bookcover:9788831793728]