• 16
• 15
• 11
• 9
• 10

# [Solved] The ? Operator

This topic is 3568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

A *lot* of time ago I was searching for something I can't remember now and found a link to an article explaining a different use of the operator "?" (not the classic ternary operation, "?:" ) Now I can't tell if it was a dream or if I really read that article. Was I dreaming? Thanks in advance. [Edited by - Blito on August 1, 2008 9:04:15 AM]

##### Share on other sites
What language? If it's C#, you might be talking about Nullable Types.

##### Share on other sites
Whether or not you were dreaming depends on which language you're talking about. In C++, ? is only an operator with :. In C#, the ? symbol is used with nullable types and ?? is used as an operator. For other languages please specify.

##### Share on other sites
In C/C++ it might have been a trigraph, e.g. ?? acts as an escape code to create control characters which some ancient terminals were lacking.
Nowadays these are only used to win obfuscated C contests and confuse poor programmers trying to compile something like this:
puts("What??!");

##### Share on other sites

Can't really tell if it was about Nullable Types, but I know I'm not giving much information about what i'm asking, so thanks again for the replies (even I don't know what I'm looking for).

I'll probably have to remember more details, or I will never find the answer.

##### Share on other sites
In C++ the ? operator could be ( a<b ? a : b ) Which returns a if the statement is true and b if its false.

##### Share on other sites
Quote:
 Original post by Net-NinjaIn C++ the ? operator could be ( a

That is the "ternary operator", which he already assured us isn't what hes talking about.

##### Share on other sites
It might've been a compiler specific extension you were seeing?
" 5.2 Minimum and Maximum Operators in C++

It is very convenient to have operators which return the "minimum" or the "maximum" of two arguments. In GNU C++ (but not in GNU C),

a <? b
is the minimum, returning the smaller of the numeric values a and b;

a >? b
is the maximum, returning the larger of the numeric values a and b.

These operations are not primitive in ordinary C++, since you can use a macro to return the minimum of two things in C++, as in the following example.

#define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))

You might then use int min = MIN (i, j);' to set min to the minimum value of variables i and j.

However, side effects in X or Y may cause unintended behavior. For example, MIN (i++, j++) will fail, incrementing the smaller counter twice. A GNU C extension allows you to write safe macros that avoid this kind of problem (see section Naming an Expression's Type). However, writing MIN and MAX as macros also forces you to use function-call notation for a fundamental arithmetic operation. Using GNU C++ extensions, you can write int min = i <? j;' instead.

Since <? and >? are built into the compiler, they properly handle expressions with side-effects; int min = i++ <? j++;' works correctly.
"

##### Share on other sites
Quote:
 Original post by daviangelThese operations are not primitive in ordinary C++, since you can use a macro to return the minimum of two things in C++, as in the following example. #define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))

There are also std::min and std::max algorithms. The difference is type-safety (among other things):
#include <iostream>#include <algorithm>#define MIN(a, b) ((a) < (b) ? (a) : (b))int main(){    int i = -10;    unsigned u = 10;    std::cout << MIN(i, u); //compiles with a warning, outputs 10!    std::cout << std::min(i, u); //compiler error, reminds you to use                                 //an explicit cast to get good results}`