How to define "

Apr 25, 2012 at 4:54pm
Is there anyway to define " ?

Example:

1
2
3
4
5
6
7
8
#define apos "

#define CONCAT(a,b,c) a ## b ## c
#define INDIR_CONCAT(a,b,c) CONCAT(a,b,c)

#define CONDITION INDIR_CONCAT(apos,This is a test,apos);

printf(CONDITION);


The thing that i'm trying to do is to make a define for " and use that define instead of using ".

printf(aposThis is a testapos);

apos means "
Apr 25, 2012 at 4:58pm
Apr 25, 2012 at 5:01pm
I already tried this, but it isn't working.
Last edited on Apr 25, 2012 at 5:02pm
Apr 25, 2012 at 5:04pm
This is a bad idea for several reasons, and I don't think it would work even if you could define something as "

 
#define CONDITION INDIR_CONCAT(apos,This is a test,apos) 


Here, even if apos is defined as " (which is a quotation mark, not an apostrpohe, just fyi), CONDITION would expand to this:

INDIR_CONCAT(",This is a test,")
which would give you a different error since you're now only passing one parameter to INDIR_CONCAT instead of the required 3.


But the bigger issue here is that this is a wretched abuse of macros.
Last edited on Apr 25, 2012 at 5:04pm
Apr 25, 2012 at 5:14pm
Mm, lets change the code like this:

1
2
3
#define text "This is a test"

printf(text);


I want to make the define called TEXT like this:

1
2
3
4
5
#define quo "

#define text quoThis is a testquo

printf(text);
Last edited on Apr 25, 2012 at 5:39pm
Apr 25, 2012 at 5:15pm
Can you explain why you are trying to do this?
Apr 25, 2012 at 5:18pm
Just curiosity. I'm working on the different define methods.
Apr 25, 2012 at 5:19pm
Even if this is allowed (which I'm not sure it is), you have the same problem.

quoThis is a different token from quo, so that wouldn't expand at all.

And quo ## This ## quo might expand to " ## This ## ", or even " ## This ## quo not "This"

What you're trying to do is just incredibly fugly and riddled with a million potential problems. Don't do it.
Last edited on Apr 25, 2012 at 5:20pm
Apr 25, 2012 at 5:25pm
The C++ preprocessor works on C++ tokens. An unclosed quote character is not a valid token.
It may work if you run the preprocessor as a stand-alone program (rather than letting the compiler call it for you)
Apr 25, 2012 at 5:26pm
Thanks for the answers. I'll be waiting to see is there anyone who can make it.
Apr 25, 2012 at 6:44pm
1
2
3
4
5
6
7
8
9
10
#define q(t) #t

#define CONDITION q(This is a test)

#include <cstdio>

int main()
{
	std::printf(CONDITION);
}
Apr 25, 2012 at 7:47pm
I know this way but the thing that i want isn't like this. Thanks anyway :), i'm going to mark the topic as solved. The last may help people who search for an answer.
Last edited on Apr 25, 2012 at 7:52pm
Topic archived. No new replies allowed.