-1
  1. Is it possible to create my own operators like '+' or '/'
  2. Is it possible in C++? I have already found operator '#' , but I do not know how to do this with another.
kam_b99
  • 37
  • 8
  • That code sample is completely invalid. Anyway, considering 2. is whether it's possible in C++, I'm assuming 1. is in general, in which case yes, some languages let you define new operators. – chris Dec 11 '15 at 21:40
  • Hey Kam some example code could help in working out what you're trying to accomplish. Also, have you read up on operator overloading? This might be a helpful place to start http://en.cppreference.com/w/cpp/language/operators if you haven't read it already – Liang Dec 11 '15 at 21:41
  • 4
    You already found `operator#`? Where? :/ – A.S.H Dec 11 '15 at 21:43
  • 1
    @A.S.H Sooner or later someone always takes a great idea and makes a hash of it. – user4581301 Dec 11 '15 at 22:17
  • @A.S.H http://stackoverflow.com/questions/7212375/create-my-own-operator-in-c – kam_b99 Dec 12 '15 at 15:58

3 Answers3

1
  1. You can overload +, - for a given class, so they have custom behavior.
  2. You can define preprocessor macros like #define OP(a,b) ((a))-(b)*(a)) and write code like 3 OP 4.

Other than that, I don't believe there's anything deep you can do in C++ to create your own new tokens or syntax. You can always write your own programming language - many people do - and creating a new operator pretty much means you're writing a new programming language by itself.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • 1
    `OP` won't be expanded in `3 OP 4`. In order to be expanded, the token after `OP` must be `(`. However, there is the idea of named operators, one option being `3 4`. – chris Dec 11 '15 at 21:47
  • 1
    Named operators are possible, but a horrible hack. You have to make "op" be a global variable of a magic type. Then you write operator <(int rhs, magictype) which returns another magic type containing the lhs argument and operator >(secondmagictype, int rhs) which actually performs the operation. – Martin Bonner supports Monica Dec 11 '15 at 21:54
  • 1
    The problem is that they often have the wrong precedence. – Martin Bonner supports Monica Dec 11 '15 at 21:55
  • 1
    @MartinBonner, Yes, they do often have the wrong precedence, and should typically be parenthesized if part of a larger expression. Being global isn't a huge problem if you can't change it because you made it `const` (much like how regular functions act). And yes, it does use expression templates. All expression template libraries have this magic. – chris Dec 11 '15 at 22:00
1

With the trivial Google search I found a table identifying 42 C++ operators that can be overloaded, and 4 C++ operators that cannot.

I did not verify this information.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2785528
  • 5,438
  • 2
  • 18
  • 20
0

If by create, you mean to define,

You can overload many defult operators for custon types, In fact for all of:

+ - * / % ˆ & | ~ ! = < > += -= *= /= %= ˆ= &= |= << >> >>= <<= == != <= >= && || ++ -- , ->* -> ( ) [ ]

Do do custom things for your classes.

You can also define functions, like float add(...) and define that to do whatever you like

Kieren Pearson
  • 416
  • 3
  • 15