Is it possible to make a custom operator so you can do things like this?
if ("Hello, world!" contains "Hello") ...
Note: this is a separate question from "Is it a good idea to..." ;)
Is it possible to make a custom operator so you can do things like this?
if ("Hello, world!" contains "Hello") ...
Note: this is a separate question from "Is it a good idea to..." ;)
Your suggestion would be nothing more than syntactic sugar for:
and in fact there are already a functions to do that in both cstring and std::string. Which is perhaps a bit like answering "is it a good idea?" but not quite; rather asking "why would you need/want to?"
Technically, no. That is to say, you can't extend the set of
operator+
,operator-
, etcetera. But what you're proposing in your example is something else. You are wondering if there is a definition of "contains" such thatstring-literal "contains" string-literal
is an expression, with non-trivial logic (#define contains ""
being the trivial case).There are not many expressions that can have the form
string-literal X string-literal
. This is because string literals themselves are expressions. So, you're looking for a language rule of the formexpr X expr
. There are quite a few of those, but they're all rules for operators, and those don't work on strings. Despite the obvious implementation,"Hello, " + "world"
is not a valid expression. So, what else can X be instring-literal X string-literal
? It can't be a expression itself. It can't be a typename, a typedef name or a template name. It can't be a function name. It can really only be a macro, which are the only remaining named entities. For that, see the "Yes (well, sort of)" answer.Yes! (well, sort of)
There are a couple publicly available tools to help you out. Both use preprocessor code generation to create templates which implement the custom operators. These operators consist of one or more built-in operators in conjunction with an identifier.
Since these aren't actually custom operators, but merely tricks of operator overloading, there are a few caveats:
_
,o
or similarly simple alphanumerics.CustomOperators
While I was working on my own library for this purpose (see below) I came across this project. Here is an example of creating an
avg
operator:IdOp
What started as an exercise in pure frivolity became my own take on this problem. Here's a similar example:
Key Differences
There's a method thoroughly explored in 'Syntactic Aspartame' by Sander Stoks that would allow you to use the following format:
In essence, you need a proxy object with the operators '<' and '>' overloaded. The proxy does all of the work; 'contains' can just be a singleton with no behavior or data of its own.
To be a bit more accurate, C++ itself only supports creating new overloads of existing operations, NOT creating new operators. There are languages (e.g., ML and most of its descendants) that do allow you to create entirely new operators, but C++ is not one of them.
From the looks of things, (at least) the CustomOperators library mentioned in the other answer doesn't support entirely custom operators either. At least if I'm reading things correctly, it's (internally) translating your custom operator into an overload of an existing operator. That makes things easier, at the expense of some flexibility -- for example, when you create a new operator in ML, you can give it precedence different from that of any built-in operator.