Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to legally write ::: in C++ and ??? in C#?

Tags:

c++

syntax

c#

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?

like image 830
nbitd Avatar asked Mar 23 '10 20:03

nbitd


2 Answers

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;
like image 178
Aaronaught Avatar answered Sep 23 '22 17:09

Aaronaught


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.

like image 37
P Daddy Avatar answered Sep 24 '22 17:09

P Daddy