-1

I am trying to create a macro wrapper around a function so I could make the code more intuitive on reading. Something like instead of calling send_message_to_destination(m, d) to write send(m)to(d).

#include <stdio.h>

void send_data_to(int data, int dest)
{
    printf("Send %d to %d\n", data, dest);
}


#define send(data)to(destination) send_data_to(data, destination) 

int main() {

    int data = 5;
    int dest = 10;

    send(data)to(dest);
}

It is possible to do so? Do you think this would makes the code more readable or intuitive ?

  • 2
    It's rare for macros to make code more intuitive, if you are using them to invent a language. – Weather Vane Mar 25 '16 at 20:12
  • 3
    This is in fact less intuitive for any C programmer. Don't try to change the grammar of C using whatever obscure way. If you don't want to use C syntax, a different language might better suit you. If there is none, write your own, but don't try this with macros. – too honest for this site Mar 25 '16 at 20:13
  • Thank you for your feedback, but due to my not so good English, I misexpressed myself. I wanted a form of writing the code that sounds more like natural language and I want to use this in c++. I personally think it is nicer to call communicator.send(message1)to(server1) than communicator.send(mesage1, server1). – brute_code Mar 25 '16 at 22:21

2 Answers2

0

I agree with the comments (not a good idea), however, you can use something like this. Remember the pre-processor just does a text replace with your macros.

#define to(destination) destination)
#define send(data) send_data_to(data,  
cleblanc
  • 3,678
  • 1
  • 13
  • 16
0

No there is no such way to do so with MACROS

Mukesh
  • 83
  • 6