Skip to main content

C++ of the Day #17 - expression? statement?

뉴스그룹에 다음과 같은 질문이 올라왔네요. ((c.l.c.m:The for-init-statement in a for statement))

Question

for ( for-init-statement conditionopt ; expressionopt ) statement
  1. C++98 문서에 보면 for expression은 위와 같이 정의되어 있는데 for-init-statement 부분을 보면 optional이 아닙니다. 그렇다면 for (;;) 와 같은 문장은 standard compliant가 아닌가요?
  2. 그리고 for-init-statement 부분을 보면 semi-colon이 빠져 있는데 표준 문서의 오류인가요?


Answer

먼저 결론부터 말하자면 위의 두 질문의 답은 모두 NO 입니다. 먼저 for-init-statement가 무엇인지는 문서의 뒤쪽에 있는 Annex A Grammar summary에서 찾을 수 있으며 다음과 같습니다. ((여기서 (opt)는 opt입니다. code 태그안에서 sub 태그가 안되서... ;-)))
for-init-statement:
expression-statement
simple-declaration

expression-statement:
expression(opt) ;

simple-declaration:
decl-specifier-seq(opt) init-declarator-list(opt) ;
보시는 바와 같이 for-init-statement는 expression-statement이거나 simple-declaration인데 둘다 semi-colon으로 끝나고 있습니다. 따라서 for 문을 정의할 때 for-init-statement뒤에 semi-colon이 없었던 것이죠.
그리고 위의 두 경우 모두 semi-colon 앞은 모두 optional임을 알 수 있습니다. 따라서 for-init-statement는 그냥 semi-colon만으로도 standard compliant한 문장이 되는 것입니다.

condition이나 expression, statement등도 이 Annex A에서 찾아볼 수 있습니다.

표준 문서를 찾아 읽다 보면 암호와 같은 문장에 좌절할 때도 많습니다. 가끔은 애매모호한 문장으로 컴파일러 회사들로 하여금 서로 다른 해석을 가능하게 하기도 하고요. 누가 표준 문서 해독에 관한 책이라도 써야 하는것 아닐까 싶네요. :-)

C++0x에는 좀 더 명확하고 쉽게 쓰여 있었으면 좋겠습니다.

참! 그리고 Sun에서 하고 있는 ABI(Application Binary Interface)같은 것들도 어디선가 표준이 만들어지면 컴파일러들간의 호환성도 좋아질텐데 하는 생각이 드네요. 물론 각각의 machine(주로 CPU)마다 ABI를 하나씩 만들어야 하는 문제가 있겠지만요. -_-;

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]