I found the below macro in an SDK sample program. What #value is meant for in this context?
#define VALUE_CHAR(value) #value, value
I found the below macro in an SDK sample program. What #value is meant for in this context?
#define VALUE_CHAR(value) #value, value
Stringification. See this page.
So
VALUE_CHAR(1)
expands to:
"1", 1
You might use this kind of macro to simplify initialization of an array, for example:
#define MYDEF(x) { #x, x }
static struct {
const char *str;
int num;
} values[] = {
MYDEF(1),
MYDEF(2),
MYDEF(3)
};
From the standard :
16.3.2 The # operator [cpp.stringize]
A character string literal is a string-literal with no prefix. If, in the replacement list, a parameter is immediately preceded by a
#
preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument.
It means that:
#define VALUE_CHAR(value) #value, value
VALUE_CHAR(some_value)
Will be expanded to :
"some_value", some_value
by the preprocessor.
For example, the famous BOOST Library uses this operator to stringize token :
#define BOOST_STRINGIZE(X) BOOST_DO_STRINGIZE(X)
#define BOOST_DO_STRINGIZE(X) #X
An example of the usage in the Test library:
#define BOOST_AUTO_TEST_SUITE( suite_name ) \
namespace suite_name { \
TheFunction( BOOST_STRINGIZE( suite_name ) ); \
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ...
BOOST_AUTO_TEST_SUITE( MyTest );
Will be expanded to:
namespace MyTest {
TheFunction( "MyTest" );
// ^^^^^^^^