nbitd
nbitd

Reputation: 301

How is it possible to legally write ::: in C++ and ??? in 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?

Upvotes: 5

Views: 717

Answers (3)

P Daddy
P Daddy

Reputation: 29537

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.

Upvotes: 1

Aaronaught
Aaronaught

Reputation: 122664

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;

Upvotes: 4

Fitzchak Yitzchaki
Fitzchak Yitzchaki

Reputation: 9163

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#.

Upvotes: 0

Related Questions