Jump to content
  • Advertisement
Sign in to follow this  
Blito

[Solved] The ? Operator

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Thanks for the answers.

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 this post


Link to post
Share on other sites
Quote:
Original post by Net-Ninja
In C++ the ? operator could be ( a<b ? a : b ) Which returns a if the statement is true and b if its false.


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by daviangel

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))


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
}


Share this post


Link to post
Share on other sites
Trigraphs have to be the most annoying thing that can befall an unsuspecting C/C++ programmer.

Other than that and the ternary operator, I don't know of anything else using a "?" in it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!