lundi 13 juin 2016

Defining macros for specific functions in C++


There are 4 things that I was wondering if I could do in C++ with macros or inline functions.

(1) postfix operations in normal types (int, char, etc)

int n = 5;
printf("%dn", n!)

output: 120

(2) infix operations in normal types (int, char, etc)

int a = 20, b = 10
if(b|a) printf("%d is divisor of %dn", b, a);
// the symbol | means "divides", this is like (a % b == 0)
output: 10 is divisor of 20

(3) sequential operations in common types

int a = 5, b = 15, c = 20;
if(a < b < c) printf("truen");
output: false
//doesn't matter if it's made with symbols or words

(4) for each (not using for(auto...) C++11) and 
it may different for lists, vectors, etc...

vector<int> v = {1,3,5,7};
foreach(e : v) printf("%d ", e);
output: 1 3 5 7 

That's it. Is it possible to make any of these macros in C++ ?

Thanks !

PS: the symbols dont need to be | or !. Since they work, they can be @ or $ or anything. The idea is to be postfix (a@), infix(a@b), sequential (a@b@c) and the foreach may be different, just to be smaller than a normal for is ok


Aucun commentaire:

Enregistrer un commentaire