Nov 21, 2013 at 3:36pm UTC
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?
Nov 21, 2013 at 3:48pm UTC
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 Nov 21, 2013 at 3:49pm UTC
Nov 21, 2013 at 3:52pm UTC
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?
Nov 21, 2013 at 4:03pm UTC
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 Nov 21, 2013 at 4:08pm UTC
Nov 21, 2013 at 4:18pm UTC
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 Nov 21, 2013 at 4:25pm UTC
Nov 22, 2013 at 7:49am UTC
There's no rating system around these parts.
A simple thanks is good enough. :-)