views:

692

answers:

4

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..." ;)

+14  A: 
Cogwheel - Matthew Orlando
One caveat: Since the pre-process stage happens before compilation, any error messages related to these custom operators have the potential to be VERY difficult to relate back to the code you wrote because the compile errors will be on whatever your code gets turned into. Not saying you shouldn't do it (if appropriate to your problem), but try to use sparingly - it's going to make your life difficult.
Michael Kohne
Sounds cool. Sounds clever. Something in the back of my head is telling me "You're doing it wrong" and "Custom operators were deliberately left out of the language spec."
Bob Kaufman
@Michael Kohne: Absolutely agree. I had some maddening debugging experiences over the last couple days.
Cogwheel - Matthew Orlando
@Bob Kaufman: yeah, it's probably better as a novelty more than anything, but if it helps make something clearer in your code it might be a Good Thing TM.
Cogwheel - Matthew Orlando
I'm guessing the ability to define new operators were left out of the language spec because it makes writing a C++ parser so much harder (and it's already pretty damn hard to begin with). You have to deal with operator precedence, associativity, etc.
Adam Rosenfield
+2  A: 

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.

Jerry Coffin
I've added a clarification/caveat to my original answer. Thanks :)
Cogwheel - Matthew Orlando
+2  A: 

Your suggestion would be nothing more than syntactic sugar for:

if( contains( "Hello, world!", "Hello" ) ...

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?"

Clifford
Well, that was just an arbitrary example I made up when I was told to split my post into a question/answer. ;)That being said, syntactic sugar is exactly the point. I love C++ because of the myriad ways you can express a solution to a problem (procedural, functional, oo, etc.). These tools give you the ability to go a step further towards representing a concept as naturally as possible.And of course there are less sober uses as well (as evidenced in IdOp examples). :P
Cogwheel - Matthew Orlando
And actually, the avg example (which I copied from the CustomOperators page) is probably a place I wouldn't use something like this. When you think about averages you think "the average of...". This makes avg(x, y) more appropriate than "x avg y". The "contains" language (which I also found on the CustomOperators page) does a better job illustrating this particular construct.
Cogwheel - Matthew Orlando
A: 

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 that string-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 form expr 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 in string-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.

MSalters