2012-04-13 11 views
35

In genere '?' operatore viene utilizzato nella forma seguente:Operatore condizionale C ('?') Con secondo parametro vuoto

A ? B : C 

Tuttavia nei casi in cui B = A ho visto la seguente abbreviazione

A ? : C 

Questo funziona sorprendentemente. È meglio lasciare il secondo parametro in (style wise), o è una loro possibilità che alcuni compilatori non siano in grado di gestirlo?

+0

Simile alla sintassi [Groovy] (http://en.wikipedia.org/wiki/Groovy_%28programming_language%29) -come. – Lion

risposta

32

Non è consentito dal linguaggio C (per quanto ne so), ma i compilatori come gcc hanno la scorciatoia a?: C come extension. a?:c significa lo stesso di a?a:c.

+18

Significa lo stesso ... con l'avvertenza che 'a' non contiene effetti collaterali. 'a?: c' esegue solo' a' una volta, mentre 'a? a: c' eseguirà gli effetti collaterali di' a' due volte. –

3

A meno che non sia un errore grave, si sta utilizzando un'estensione del compilatore (ad esempio, gcc). Sono abbastanza sicuro che lo standard non consenta di omettere il secondo operando sull'operatore ternario.

16

la sua estensione di un gcc

Conditionals with Omitted Operands

x ? : y è equivalente a x ? x : y

+2

La pagina che hai collegato si contraddice. Da un lato si dice "Questo esempio è perfettamente equivalente a' x? X: y' ", il che significa che' x' viene valutato due volte, ma d'altra parte l'ultimo paragrafo afferma che 'x' sarà valutato solo una volta, che lo renderebbe perfettamente equivalente a 'x || y', non 'x? x: y' – Celada

+1

@ Celada: Penso che significhi dire che 'x? : y' è ** approssimativamente ** equivalente a 'x? x: y' tranne 'x' viene valutato solo una volta nel primo caso. –

+3

@ Celada: 'x || y' valuta a 0 o 1, che non è il caso di questo operatore. –

0

E 'meglio lasciare il secondo parametro. Se B cambia mai, non si può ricordare di modificare la dichiarazione di cui sopra . Inoltre, altre persone potrebbero avere difficoltà a leggere il tuo codice e migliorarlo se lasci B fuori da questa affermazione.

1

ho fatto una piccola ricerca nel web, di tronchi con a wikipedia, questo comportamento è supportato da un'estensione GNU di C. http://en.wikipedia.org/wiki/%3F:#C

quindi è molto probabile che altri compilatori considerano questo illegale. A proposito, questo operatore è chiamato condizionale ternario in modo da poter navigare su di esso.

EDIT:

ho controllato in gcc e mela LLVM e funziona benissimo.

3

Compilare un po '.

Lo standard utilizza il termine operatore condizionale.

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

un'espressione condizionale non produce un lvalue. Inoltre; Wikipedia; Conditional

Nota: cioè .: C++ ha:
        logico-O-espressione?espressione: assegnazione cibo -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. 

Piede:

[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

Quindi: non è saggio da usare.