You are on page 1of 4

SOMETHING THAT I WOULD LIKE TO HAVE IN C AND C++

INTRODUCTION
Okay, now it comes something I would like to share with other people. It came into spot light long time
ago, however it has been magnified some time ago. Actually, while I was developing the calculator
application, it had come back.
Now, I wonder, would this thing happened one day the way I would like it to happen. So, let's stop bitting
about bush and consider what I was talking about.

MAIN PART
Now if we would like to add two numbers we could write function like this:
double add_function ( double a, double b) { return a + b; }
and if you would like to multiply you would create function like this
double multiply_function ( double a, double b){ return a * b;}
Then you could add some function with the function pointers like this:
double
some_operation( double first, double (* f ) ( double, double), double right)
{
return (*f) (first, second );
}
Now in the main function you would do something like this:
...
some_operation( value_1, add_function, value_2);
...
some_operation( value_1, multiply_function, value_2);

Now, you could add functions: subtract, and divide. After that, you would be having templates in mind
and how to do it in C++ in the most efficient way.
Well, who like to type if we could create more flexible function.
Then you might get idea to write function like this:
double
binary_operation( double first, char op, double second )

double dResult = 0;
switch ( op )
{
case '+': dResutl = first + second;
break;
case '*': dResutl = first * second;
break;
case '-': dResutl = first - second;
break;
case '/': dResutl = first / second;
break;
}

return dResult;

And you would call it like this:


double Result = binary_operation( number1, op , number2 );
You could omit number 1 and number 2 syntax to stay up to main stream syntax, however I prefer this
way for the sake of clarity. However, I don't recommend this practice in professional application, due to
issues with syntax.
Now, you would like to say that you have accomplish nice stuff, however if you are having bigger picture
in the mind, at this moment, you would wish for something else.
On the other hand, if you are C++ developer, you would solve this with overloaded operators and some
more tricks from C++, and you would be able to think of a parser on your own.
Somehow, you would ask you self could I do it in the more efficient way, I mean could I type less for
more flexibility?
Now let's sideline from this thread of story and observe this piece of code:
#include <stdio.h>
#include <stdlib.h>
#define TEXT_MOST "Hello, world"
#define TEXT_MYLANGUAGE "Zdravo, svete"
#define TEXT_FRIEND "Hola, el mundo"
#define msg(x) puts( TEXT_ ##x)

int
main( void )
{
msg(MOST);
msg(MYLANGUAGE);
msg(FRIENDLANGUAGE);
return EXIT_SUCCESS;
}
As you have noticed, well I hope, we have added two string into one in order to create one macro, and
now if we have function-like macros we would be able to create something that will have macro and
function in one.
That does the job, but creates rather ugly syntax.
So, not happy yet.
There comes the way, which is still not accomplished by me yet.
How would I like to do it, well like this:
double nice_binary_operation ( double first, char op, double second)
{
char[] body =;
strcat( body, itoa( first ) );
strcat(body, op);
strcat( body, itoa( second );
return body;
}
This syntax is still pretty ugly. However it would make me way more happy to accomplish something like
this.

CONCLUSION
Now, there is a time to ask you self a question, how would you use this tool in real life scenario. Well,
let say that you would like to create the math expression parser. In order to accomplish that you would
need to write complex things like: binary expression, unary expression, functions that you use and
stuff.
If I could write what I need, I would be able to create something more elegant and more flexible.
So, user inputs the expression and you have math expression and the result is executed after you get the
input.

Well, it could be done, but it is not nice... There is a lot of work... and I would rather to some other
stuff than
You know, like creating dynamic expressions that act like functions.

You might also like