 
    
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>## Archives - Complete, Concrete, Concise</title>
	<atom:link href="https://complete-concrete-concise.com/tag/184/feed/" rel="self" type="application/rss+xml" />
	<link>https://complete-concrete-concise.com/tag/184/</link>
	<description>Practical Information Without The Bloat</description>
	<lastBuildDate>Tue, 07 Jun 2011 06:35:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Preprocessor &#8211; The Token Pasting (##) Operator</title>
		<link>https://complete-concrete-concise.com/programming/c/preprocessor-the-token-pasting-operator/</link>
		
		<dc:creator><![CDATA[richardsplanet]]></dc:creator>
		<pubDate>Tue, 07 Jun 2011 06:35:50 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[##]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[token pasting]]></category>
		<category><![CDATA[understanding]]></category>
		<guid isPermaLink="false">http://complete-concrete-concise.com/programming/c/preprocessor-the-token-pasting-operator</guid>

					<description><![CDATA[<p>This is probably the least understood and most poorly documented preprocessor operator. The token pasting (##) operator simply eliminates any white space around it and concatenates (joins together) the non-whitespace characters together. It can only be used in a macro definition. It is used to create new tokens. It may not be the first or [&#8230;]</p>
<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-token-pasting-operator/">Preprocessor &#8211; The Token Pasting (##) Operator</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="c1">
<p>This is probably the least understood and most poorly documented preprocessor operator.</p>
</div>
<p>The token pasting (<code>##</code>) operator simply eliminates any white space around it and concatenates (joins together) the non-whitespace characters together. It can only be used in a macro definition. It is used to create new tokens.</p>
<p>It may not be the first or last characters in the replacement text.</p>
<p>If there are multiple token pasting operators (<code>##</code>) and / or stringizing operators (<code>#</code>) the order of evaluation is undefined.</p>
<p>Valid tokens are:</p>
<ul>
<li>identifiers (variable names, function names, etc)</li>
<li>keywords (int, while, volatile, etc)</li>
<li>literals (strings, numbers, characters, true or false)</li>
<li>operators and punctuators (+, -, *, (, etc)</li>
</ul>
<p>If the result of using <code>##</code> is not a valid token, then the behaviour is undefined.</p>
<p>Consider the following macro definition which uses the ## operator to remove the extra spaces in the replacement string:</p>
<p><code></p>
<pre>#define macro_start i ## n ##t m         ##ain(void)</pre>
<p></code></p>
<p>After preprocessing the name macro_start will be replaced by:</p>
<div class="c2"><code>int main(void)</code></div>
<p>because the preprocessor will have processed the <code>##</code> by</p>
<ol>
<li>removing all instances of it, </li>
<li>eliminating white space surrounding it, </li>
<li>concatenating the non-whitespace characters together.</li>
</ol>
<p>While the token pasting (<code>##</code>) operator can be used with parameterless macros, it doesn&#8217;t make any sense to do so: you can just type what you need without using the <code>##</code> operator to concatenate it.</p>
<p>It&#8217;s true power becomes evident when you use it to concatenate the parameters you pass in the macro. (As far as the preprocessor is concerned, everything it handles is just a bunch of text).</p>
<p>Normally, it is used for automatically creating new identifiers. For example,</p>
<div class="c2"><code>#define my_macro(x) x##_macrofied</code></div>
<p>will concatenate the parameter passed (<code>x</code>) and append the suffix <code>_macrofied</code> to it.</p>
<div class="c2"><code>my_macro(identifier)</code></div>
<p>will be expanded into:</p>
<div class="c2"><code>identifier_macrofied</code></div>
<h1>Compiler Differences in handling ##</h1>
<p>GCC and Visual C++ handle <code>##</code> ambiguity differently. </p>
<p>GCC is strict if the resulting concatenation is not a valid preprocessing token &#8211; it issues an error during compilation.</p>
<p>Visual C++, on the other hand,  reprocesses the concatenation result and will accept constructs that are deemed invalid by GCC. </p>
<p>Both compilers are working correctly, since the standard does not define how an invalid token is to be handled (it just says the behaviour is undefined). Rejecting it is fine as is reprocessing the result and parsing it into valid tokens.</p>
<h2>Example:</h2>
<p>The following will fail with GCC but succeed with Visual C++</p>
<div class="c2"><code>#define macro_start int main ## (void)</code></div>
<p>After the preprocessor has concatenated <code>main</code> and <code>(</code> we have the token</p>
<div class="c2"><code>main( </code></div>
<p>which is not a valid token. </p>
<p>GCC rejects it.</p>
<p>On the other hand, Visual C++ reprocesses it producing two tokens: 1) an identifier <code>main</code> and 2) the punctuator / operator <code>(</code>.</p>
<p>Both compilers will correctly process the following</p>
<div class="c2"><code>#define macro_increment(x) x+ ## +</code></div>
<p>because it parses into 2 tokens. The first being the identifier <code>x</code>, the second being the operator <code>++</code>.</p>
<h1>Why Use it?</h1>
<p>It is generally used to reduce repetitive (thus error prone) typing.</p>
<p>The folowing code sample defines a macro that creates a new scalar type in C / C++ and creates 6 custom named functions for that type (initialization, addition, subtraction, multiplication, division, and raw value access). Without use of the <code>##</code> operator, this would have to either been typed in (very repetitive, boring and error prone if you are defining many types) or done via a copy / paste / search and replace operation (still repetitive, boring and error prone if you are defining multiple types).</p>
<p><code></p>
<pre>#define new_scalar_type(name, type) \
typedef struct \
{ \
    type value; \
} name; \
inline name name##_(type v) \
{ \
    name t; \
    t.value = v; \
    return t; \
} \
inline name add_##name(name a, name b) \
{ \
    name t; \
    t.value = a.value + b.value; \
    return t; \
} \
inline name sub_##name(name a, name b) \
{ \
    name t; \
    t.value = a.value - b.value; \
    return t; \
} \
inline name mul_##name(name a, name b) \
{ \
    name t; \
    t.value = a.value * b.value; \
    return t; \
} \
inline name div_##name(name a, name b) \
{ \
    name t; \
    t.value = a.value / b.value; \
    return t; \
} \
inline type value_##name(name a) \
{ \
    return a.value; \
} \
</pre>
<p></code></p>
<p>When you use the macro in your code:</p>
<div class="c1"><code>new_scalar_type(age, int);</code></div>
<p>you get</p>
<ul>
<li>a new type called <code>age</code>, represented by an <code>int</code></li>
<li>an initializing function <code>age age_(int t)</code></li>
<li>an addition funtion <code>age add_age(age a, age b)</code></li>
<li>a subtraction function <code>age sub_age(age a, age b)</code></li>
<li>a multiplication function <code>age mul_age(age a, age b)</code></li>
<li>a division function <code>age div_age(age a, age b)</code></li>
<li>a raw value access function <code>int value_age(age a)</code></li>
</ul>
<p>If you defined a new type, say <code>weight_lbs</code>, then you will get the new type and the associated, custom named, functions that go along with it.</code></p>
<p>You can now define a new type and use it as follows:</p>
<p><code></p>
<pre>new_scalar_type(age, int);
int main (void)
{
    age a = age_(42);
    age b = age_(24);
    age c = add_age(a, b);
    return value_age(c);
}</pre>
<p></code></p>
<h1>Another Example</h1>
<p>The following macro (called <code>convert</code> can be used to automatically generate a function that converts from one value to another (for example, &deg;F to &deg;C or feet to inches, etc):</p>
<pre>#define convert(from, to, conversion, from_type, to_type) \
to_type convert_##from##_to_##to(from_type f) \
{ \
   return conversion; \
} \</pre>
<p>It takes 5 parameters:</p>
<ol>
<li><code>from</code> &#8211; a descriptive name of the unit we are converting from</li>
<li><code>to</code> &#8211; a descriptive name of the unit we are converting to</li>
<li><code>conversion</code> &#8211; the conversion equation (yes, macro parameters can be complex)</li>
<li><code>from_type</code> &#8211; the type we are converting from</li>
<li><code>to_type</code> &#8211; the type we are converting to</li>
</ol>
<p>The macro would be used as follows:</p>
<pre>convert(f, c, (f-32)*5.0/9.0, float, float);
convert(ft, in, ft * 12, int, int);</pre>
<p>When the macros are expanded, we get two functions called <code>convert_f_to_c</code> and <code>convert_ft_to_in</code>. These can be used as follows:</p>
<pre>int main (void)
{
    float a = 70.0;
    float b;
    int c = 3;
    int d;
    b = convert_f_to_c (a);
    d = convert_ft_to_in(c);
    return 0;
}</pre>
<div class="c1">
<p><strong>Historical note:</strong> some (very) old compilers accepted <code>/**/</code> for token pasting (this was before the time of the C89 standard). If you are browsing through some old C code, you might find code like this:</p>
<pre>#define my_macro(x, y) x/**/y</pre>
<p>This worked because the preprocessor eliminates whitespace. If the particular implementation eliminated whitespace <u>after </u>parameter replacement, then it would concatenate the parameters.</p>
<p>I know of no compilers that accept this.</p>
</div>

<p>The post <a href="https://complete-concrete-concise.com/programming/c/preprocessor-the-token-pasting-operator/">Preprocessor &#8211; The Token Pasting (##) Operator</a> appeared first on <a href="https://complete-concrete-concise.com">Complete, Concrete, Concise</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
