Is it possible to abuse syntax colon in C++ for specific use?

  Kiến thức lập trình


Is there another way to use the colon symbol : in an unexpected way (humorous or very very specific application) ?

C++ have so many unexpected things !

We all know that C++ dev have many ways to do fun things around by abusing things that people didn’t think it were possible

You want to have a url in your code ? sure:
You want to avoid opt-in deduce ? sure:
You want to try catch during init ? sure:
You want to run a func before init ? sure: C++ – Run a function before initializing a class member
You want to know different ways of initialization ? sure:
You want to find some non-trivial topics ? sure:

There are many possible syntactic sugar of C++.

Here are some examples

You want operator <=> ? sure: What is the <=> (“spaceship”, three-way comparison) operator in C++?
You want operator / for concatenating filepath ? sure:
ossible operator | and |> ? sure:
You want many dots `……` ? sure: What is the meaning of “… …” token? i.e. double ellipsis operator on parameter pack
You want Cppfuck language -~!<[]{} ? sure:
You want your own operator _literal ? sure: What new capabilities do user-defined literals add to C++?

I want to somehow abuse the C++ features for creating new syntax.

The majority of them can be considered too bad to be applicable for production, but some can be useful in very specific tasks that occasionally find us discovering better alternatives about.

You want operator --> ? sure: What is the ‘–>’ operator in C/C++?
You want operator <- ? sure:
You want operator [x,y,z] ? sure: Comma Operator in subscript operator?
You want operator --*, --()->, … ? sure
You want operator <div>, <mod>, … ? sure:
You want operator -dot-, *cross*, … ? sure: (broken link, lmao)


One thing to notice that it is not an operator, overloadable (ternary), nor being able to be defined using #define in a traditional way.

How can I abuse colon : in C++, are there any humurous way ? Are there any possible usecases (either unintended or very very specific) ?

One of possible application is making a range Range [left:right]

AFAIK, these are where colon can be used?

  1. switch statement

  2. range based loop

  3. class initializer list

  4. class inheritance

  5. class access specifier

  6. goto label

  7. bitfield

  8. ternary operator ?:

  9. labels for attribute

  10. scope resolution (technically double colon)

  11. C++26 reflection splicer proposal

  12. C++26 ternary operator proposal

  13. C++ designator initializer proposal

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website