Is there a way to get the C++ pre-processor to expand a #define'ed value into a string literal?
for example:
#define NEW_LINE '\n'
Printf("OutputNEW_LINE"); //or whatever
This looks to me like it should be possible as it's before compilation?
Or is there a better design pattern to achieve this kind of behaviour (without resorting to runtime fixes like sprintf)?
EDIT I understand that #define's can be evil, but for arguments sake...
ADDITIONAL Does anyone have any criticism of this approach?
Definition of expand on/upon : to speak or write about (something) in a more complete or detailed way : elaborate on She declined to expand on her earlier statement. Please expand on that idea. Researchers will expand upon their data in a new study.
to give more details about something you have said or written: I wonder if you would like to expand on your earlier remarks.
to give more details about something said or written: He studied with Sigmund Freud and expanded upon many of his theories. Want to learn more?
He has expanded his business to serve the entire state. There are plans to expand the airport. The police have decided to expand their investigation. She plans to expand the lecture series into a book.
This will do it:
#define NEW_LINE "\n" // Note double quotes
Printf("Output" NEW_LINE);
(Technically it's the compiler joining the strings rather than the preprocessor, but the end result is the same.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With