2012-04-13 14 views
35

通常、 '?'オペレータは、次の形で使用される:空の第2パラメータを含む条件演算子( '?')

A ? B : C 

ただしB = Aの場合に私は、次の略語

A ? : C 

これは驚くべき動作を見ています。 2番目のパラメータを(スタイル・ワイズ)のままにしておく方が良いですか、それとも特定のコンパイラがこれを処理できない可能性がありますか?

+0

[Groovy](http://en.wikipedia.org/wiki/Groovy_%28programming_language%29)のような構文です。 – Lion

答えて

32

言語Cでは(これまで知っている限り)許可されていませんが、gccなどのコンパイラにはショートカットa?:cがextensionとしてあります。 a?:cは、a?a:cと同じ意味です。

+18

「a」に副作用が含まれていないという警告と同じ意味です。 'a?:c'は' a'を一度だけ実行し、 'a?a:c'は' a'の副作用を2回実行します。 –

3

私が間違っていない限り、コンパイラの拡張機能を使用しています(gccの推測で)。私はかなり標準であると確信していますではなく、第3オペランドの第2オペランドを省略することができます。

16

そのGCCの拡張は

Conditionals with Omitted Operands

x ? : yx ? x : y

+2

リンクしたページが矛盾します。一方、「この例は 'x?x:y'と完全に等価です」ということは、' x'が2回評価されることを意味しますが、最後の段落は 'x'が1回だけ評価されることを意味し、それは 'x ||と完全に等しくなります。 y'ではなく 'x? x:y' – Celada

+1

@Celada:「x? :y'は**ほぼ** xと同じですか? x:yは、前者の場合には1回だけ評価されます。 –

+3

@Celada: 'x || y 'は0または1と評価され、この演算子では当てはまらない。 –

0

第二のパラメータを残して優れていると等価である。Bはこれまで変更された場合、あなたは上記のステートメントを変更することを覚えてないかもしれません。さらに、他の人々は、Bを声明の中に残すと、あなたのコードを読んでそれを改善するのが難しいかもしれません。私はウィキペディアた内容に応じて、ウェブで少し研究をした

1

、この動作はC. http://en.wikipedia.org/wiki/%3F:#CのGNU拡張

によってサポートされているので、他のコンパイラはこれが違法考えることは非常に可能性があります。ちなみに、この演算子は三項条件と呼ばれ、あなたはそれについて閲覧することができます。

EDIT:

私はgccとリンゴLLVMでチェックし、それが正常に動作します。

3

少し記入してください。

標準では、用語を条件演算子と使用しています。

Syntax 
    conditional-expression: logical-OR-expression logical-OR-expression? expression : conditional-expression 

条件式は左辺値を生成しません。また、 Wikipedia; Conditional

注:すなわち:C++があります
       論理OR式を?表現:割り当て -expression

Constraints: 
* The first operand shall have scalar type[1]. 
* One of the following shall hold for the second and third operands: 
    — both operands have arithmetic type[2]; 
    — both operands have the same structure[3] or union type[4]; 
    — both operands have void type[5]; 
    — both operands are pointers to qualified or unqualified[6] versions of compatible 
    types[7]; 
    — one operand is a pointer and the other is a null pointer constant[8]; or 
    — one operand is a pointer to an object or incomplete type[9] and the other 
    is a pointer to a qualified or unqualified version of void. 

フット食べ物:

[1] Scalar type  : Arithmetic types and pointer types. 
[2] Arithmetic type : Integer and floating types. 
[3] Structure type : A sequentially allocated nonempty set of member objects (and, in 
        certain circumstances, an incomplete array), each of which has an 
        optionally specified name and possibly distinct type. 
[4] Union type  : An overlapping nonempty set of member objects, each of which has 
        an optionally specified name and possibly distinct type. 
[5] Void type  : An empty set of values; it is an incomplete type that cannot be 
        completed. 
[6] Qualified type : 1998 (const and volatile), 1999 (restrict), respectively 
        2011 (_Atomic). * 
[7] Compatible type : Their types are the same. 
[8] Null ptr. const.: NULL; implementation-defined null pointer constant. 
[9] Incomplete type : Types that describe objects but lack information needed to determine 
         their sizes. 

* Type qualifiers in C

ので:使用する賢明ではありません。