[macro] - concat strings

i'm creating a nice macro.

for do from here

cout << (string)"hello " + "world";

to here:

cout << "hello " + "world";

but for these macro isn't correct :(

#define (a+b) ((string) a+b)
(of course the 'a' and 'b' are literal strings or char*)

error message:
"error: macro names must be identifiers"

all types of macros must be like we use a function or these macro can be fixed?
No, you can't use it like that. Macros need identifiers.

To be honest, a macro isn't going to help you here. It's quite convoluted for the purpose it serves and it doesn't really save you any time:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

#define ADD_STR( a,b ) ( std::string ) a + b 

int main( int argc, char* argv[] )
{
    std::cout << ADD_STR( "Hello", " world!" );
    return 0;
}


It's easier to achieve what you want by just using the stream operator again.

std::cout << "Hello" << " world " << std::endl;
Last edited on
so with my new language i can use '+' but then i tansform it in '<<' or ','(because of my own function).
'Macros need identifiers.' - like functions names and how we use them?
if that macro would work it'd replace all + with your expression because a macro (preprocessor) knows nothing about types.

Just one of millions of side effects:

int i = 1 + 2; becomes int i = ((string) 1+2);

and it's really not necessary. You can write it like so:

cout << "hello " "world"; // Prints: hello world
Last edited on
thanks i never knew about that.. thanks to both
i can't find:
- where i rate you?
(i'm new with these forum)
Last edited on
There's no rating system around these parts.

A simple thanks is good enough. :-)
thank you for all to all
Topic archived. No new replies allowed.