C ++中復合語句和塊之間的區別? (Difference between a compound statement and a block in C++?)


問題描述

C ++中復合語句和塊之間的區別? (Difference between a compound statement and a block in C++?)

我經常聽說“複合語句”這個詞幾乎可以互換地用於表示塊,以 {} 的大括號開始和結束。我看不出一個塊有什麼不同。

所以我的問題是:

  • “塊”和“複合語句”之間有區別嗎?

參考解法

方法 1:

To cite the actual C++ standard, ISO/IEC 14882:2017 (C++17), Section 9.3 "Compound statement or block":

9.3 Compound statement or block [stmt.block]

1 So that several statements can be used where one is expected, the compound statement (also, and equivalently, called “block”) is provided.

compound‑statement: { statement‑seqopt }

there is no difference between the terms of a block and a compound statement in their respective meaning.

You can use both terms interchangeably for a "compound" or sequence of statements (it may even be only one statement; the amount of statements doesn´t matter) starting by a "{" ‑ opened curly bracket and terminating by a "}" ‑ closed curly bracket.

方法 2:

A "block" of code, delimited by { } to indicate entry‑exit points, limits the liftetime of any variables defined inside the block to that block, wheras a "compound statement" might not unless it uses { } inside it. A function definition may be construed as a "named block" of code with an entry and exit points.

(by RobertS supports Monica CellioRobertS supports Monica Celliosrinivirt)

參考文件

  1. Difference between a compound statement and a block in C++? (CC BY‑SA 2.5/3.0/4.0)

#terminology #scope #C++ #statements






相關問題

抽象 ViewModel 在被繼承時是否被視為模型? (Is an abstract ViewModel considered a Model when it is inherited?)

什麼是 Lambda? (What is a Lambda?)

具體確定項目順序的排序的正確術語是什麼? (What is the proper term for an ordering where the order of items is concretely determined?)

在 Ruby 中,“接收者”指的是什麼? (In Ruby what does the "receiver" refer to?)

錯誤跟踪和問題跟踪系統有什麼區別? (What's the difference between a bug tracking and an issue tracking system?)

為什麼術語 API 和 SDK 似乎可以互換使用? (Why do the terms API and an SDK seem to be used interchangeably?)

Java 中的對等類是什麼? (What is a peer class in Java?)

模擬和模擬有什麼區別? (what is the difference between Emulate and Simulate?)

協議術語:消息與數據包 (Protocol Terminology: Message versus Packet)

表示“目錄”或“文件”的詞是什麼? (What is the word that means "directory" or "file"?)

C ++中復合語句和塊之間的區別? (Difference between a compound statement and a block in C++?)

gnu八度中gnu的含義? (Meaning of gnu in gnu octave?)







留言討論