TL;DR
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: https://www.reddit.com/r/programming/comments/akivf/comment/c0i1y4i/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
You want to avoid opt-in deduce ? sure: https://artificial-mind.net/blog/2020/09/26/dont-deduce
You want to try catch during init ? sure: https://weseetips.wordpress.com/tag/exception-from-constructor-initializer-list/
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: https://www.reddit.com/r/ProgrammerHumor/comments/8nn4fw/forrest_gump_learns_c/
You want to find some non-trivial topics ? sure: https://fouronnes.github.io/cppiceberg/
…
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: https://en.cppreference.com/w/cpp/filesystem/path/append
ossible operator |
and |>
? sure: https://www.reddit.com/r/cpp/comments/iso0fp/p2011_a_pipelinerewrite_operator_what_are_your/
You want many dots `……` ? sure: What is the meaning of “… …” token? i.e. double ellipsis operator on parameter pack
You want Cppfuck language -~!<[]{}
? sure: https://www.youtube.com/watch?v=SnKGD-AoKoA&t=112s
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: https://www.atnnn.com/p/operator-larrow/
You want operator [x,y,z]
? sure: Comma Operator in subscript operator?
You want operator --*
, --()->
, … ? sure http://rsdn.org/forum/humour/3686634.flat
You want operator <div>
, <mod>
, … ? sure: https://github.com/klmr/named-operator
You want operator -dot-
, *cross*
, … ? sure: (broken link, lmao)
Question
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?
-
switch statement
-
range based loop
-
class initializer list
-
class inheritance
-
class access specifier
-
goto label
-
bitfield
-
ternary operator
?:
-
labels for attribute
-
scope resolution (technically double colon)
-
C++26 reflection splicer proposal https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2996r0.html#splicers
-
C++26 ternary operator proposal https://web-docs.gsi.de/%5C~mkretz/D0917.pdf
-
C++ designator initializer proposal https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2287r1.html