tags:

views:

335

answers:

3

These questions are a kind of game, and I did not find the solution for them.
It is possible to write ::: in C++ without using quotes or anything like this and the compiler will accept it (macros are prohibited too).

And the same is true for C# too, but in C#, you have to write ???.

I think C++ will use the :: scope operator and C# will use ? : , but I do not know the answers to them.

Any idea?

A: 
I think C# will use ? :

Do you mean use three question marks in the same line?

var a = true ? new Nullable<int>(1) ?? 1 : 0;

Edit: as far as I know, it's impossible to write ??? in any version of C#.

Mendy
+4  A: 

You can write three consecutive question marks in C# without quotes, but not without whitespace, using the null-coalescing operator and the nullable alias character:

object x = 0;
int y = x as int? ?? 1;
Aaronaught
+1  A: 

With whitespace, it's easy:

C++

class A{};
class B : :: A{};

or

int foo;

int bar(){
    return decision ? -1 : :: foo;
}

But without whitespace, these won't compile (the compiler sees :: :, which doesn't make any sense).

Similarly, Aaronaught gave a good example of ? ?? in C#, but without whitespace, the compiler sees it as ?? ?, which won't compile.

P Daddy
Do you have a reference that C# compiler see `???` as `?? ?` ? It's make more scene that the compiler don't *see* it at all.
Mendy
@Mendy: Without whitespace (`???`), the compiler gives two errors: "Invalid expression term '?'", located at the third question mark, and "; expected" located after it. If you put a space in so that it's `?? ?`, you get the same two errors at the same [relative] locations. It's essentially the "maximum munge" principal that user168715 mentioned in a comment to the original question.
P Daddy