The are mag case where you may want to [paste] tokens, one example form
some code I'm working on:
void check(char const* fn, int result, char const* file, int line);
#define CheckOk(fn, args) check(#fn, fn##args, __FILE__, __LINE__)
Which passes the function name and the result of its call to the
function check().
If you mean to use this as, e.g.:
CheckOk(foo, (1, 2.5))
which should pass to check() these four arguments:
- the string "foo" (or more precisely, the address of the 'f' in
this string),
- the result of calling foo(1, 2.5),
- the source file, and
- the source file line number
then the macro should be defined *without* using the token-pasting
operator. Pasting together the two tokens <foo> and <(> results
in the invalid pp-token <foo(>, which gives undefined behavior.
(Thus, it may or may not work, depending on the implementation.)
"Real" examples of "proper" use of token pasting are relatively
rare. One can construct artificial examples easily enough though --
for instance, your CheckOk macro might be used instead to call
two variants of every function name:
#define Check2(fn, args) \
(check(#fn "1", fn ## 1 args, __FILE__, __LINE__), \
check(#fn "2", fn ## 2 args, __FILE__, __LINE__))
The expansion of Check2(foo, (1, 2.5)) is then:
check("foo" "1", foo1 (1, 2.5), "sourcefile.c", 42),
check("foo" "2", foo2 (1, 2.5), "sourcefile.c", 42)
(with the expansion occupying a single line, broken into two
lines only for newsgroup posting purposes).