Pregunta

Let's say I have the following code

int i;
int j;
string s;
string t;

Is there any preprocessers that can convert it to something like this?

int i;
int j;
string s;
string t;
void print() {
    cout << i;
    cout << j;
    cout << s;
    cout << t;
}

Something that looked like this would be great.

#section print
int i;
int j;
string s;
string t;
#endsection

With the above code I would have to write another preprocessor that handles it but I do not know how.

¿Fue útil?

Solución

No.

Preprocessor-directives are defined in preprocessor, and You can not add new ones.

But You can easily write some script that would make such changes. (Per/Python/Ruby, whatever)

Only minus would be need for manually running that script before each compilation.

Otros consejos

This is hardly what I'd call "great style" but a technique I use in a pinch, especially in restricted systems, called XMacro

Basically, make a file with an extension like .xmacro (I will use MyXMacro.xmacro) then fill it with data like this:

XMACRO_INT(i)
XMACRO_INT(j)
XMACRO_STRING(s)
XMACRO_STRING(t)
#undef XMACRO_INT
#undef XMACRO_STRING

Then in your code you write something like this:

#define XMACRO_INT(x) int x ;
#define XMACRO_STRING(x) std::string x ;
#include "MyXMacro.xmacro"

void print() {
# define XMACRO_INT(x) std::cout << x ;
# define XMACRO_STRING(x) std::cout << x ;
# include "MyXMacro.xmacro"
}

At a certain point though, writing a script and incorporating it into your build is a better choice.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top