<?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>metaprogramming | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/metaprogramming/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Fri, 20 Nov 2020 11:45:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>metaprogramming | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>C++ Template Metaprogramming Game</title>
		<link>https://learncgames.com/c-template-metaprogramming-game/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c-template-metaprogramming-game</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sun, 22 Nov 2020 00:00:15 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[template]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1857</guid>

					<description><![CDATA[<p>Templates in C++ are a useful feature. Without them you&#8217;d not have template functions, or more usefully template classes like vector etc.  But there is an even more useful feature called template metaprogramming. It&#8217;s a very advanced and clever feature; one I have never done and I don&#8217;t think anyone could say they&#8217;ve mastered C++ [&#8230;]</p>
The post <a href="https://learncgames.com/c-template-metaprogramming-game/">C++ Template Metaprogramming Game</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:359,&quot;href&quot;:&quot;https:\/\/pixabay.com\/users\/openclipart-vectors-30363\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=159135&quot;,&quot;archived_href&quot;:&quot;&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[],&quot;broken&quot;:false,&quot;last_checked&quot;:null,&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:360,&quot;href&quot;:&quot;https:\/\/pixabay.com\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=159135&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260205102120\/https:\/\/pixabay.com\/?utm_source=link-attribution&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:23:08&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-14 00:12:35&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-17 00:34:21&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-02-26 12:31:42&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-06 10:33:42&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-11 09:06:13&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-03-18 15:22:15&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-14 14:13:10&quot;,&quot;http_code&quot;:403},{&quot;date&quot;:&quot;2026-04-22 17:21:58&quot;,&quot;http_code&quot;:403}],&quot;broken&quot;:true,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-22 17:21:58&quot;,&quot;http_code&quot;:403},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:361,&quot;href&quot;:&quot;https:\/\/en.wikipedia.org\/wiki\/Template_metaprogramming&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260202182013\/https:\/\/en.wikipedia.org\/wiki\/Template_metaprogramming&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:23:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-13 03:13:46&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 00:34:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-22 03:08:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 12:31:42&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-06 05:37:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-11 09:06:13&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-18 15:22:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-22 06:15:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-31 00:59:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-13 01:07:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-21 02:28:00&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-21 02:28:00&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:362,&quot;href&quot;:&quot;https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-template-metaprogramming&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20251213062742\/https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-template-metaprogramming\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:23:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-14 00:12:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 01:05:16&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 12:31:47&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-06 10:33:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-11 09:06:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-18 15:22:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-14 14:13:11&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-22 17:22:00&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-22 17:22:00&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:363,&quot;href&quot;:&quot;https:\/\/github.com\/mattbierner\/STT-C-Compile-Time-Snake&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260109101813\/https:\/\/github.com\/mattbierner\/STT-C-Compile-Time-Snake&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:23:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-15 08:10:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-18 15:22:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-14 05:29:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-22 17:21:58&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-22 17:21:58&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<figure id="attachment_1860" aria-describedby="caption-attachment-1860" style="width: 229px" class="wp-caption alignleft"><img fetchpriority="high" decoding="async" class="size-medium wp-image-1860" src="https://learncgames.com/wp-content/uploads/2020/11/rattlesnake-159135_640-229x300.png" alt="Snake" width="229" height="300" srcset="https://learncgames.com/wp-content/uploads/2020/11/rattlesnake-159135_640-229x300.png 229w, https://learncgames.com/wp-content/uploads/2020/11/rattlesnake-159135_640.png 488w" sizes="(max-width: 229px) 100vw, 229px" /><figcaption id="caption-attachment-1860" class="wp-caption-text">Image by <a href="https://pixabay.com/users/openclipart-vectors-30363/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=159135">OpenClipart-Vectors</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=159135">Pixabay</a></figcaption></figure>
<p>Templates in C++ are a useful feature. Without them you&#8217;d not have template functions, or more usefully template classes like vector etc.  But there is an even more useful feature called <a title="Link to Wikipedia.org on Template MetaProgramming" href="https://en.wikipedia.org/wiki/Template_metaprogramming" target="_blank" rel="nofollow noopener noreferrer">template metaprogramming</a>. It&#8217;s a very advanced and clever feature; one I have never done and I don&#8217;t think anyone could say they&#8217;ve mastered C++ unless they are good at it.</p>
<p>Here&#8217;s an example of the simplest example I could find. It comes from <a title="Linbk to example of template metaprogramming" href="https://www.modernescpp.com/index.php/c-core-guidelines-rules-for-template-metaprogramming" target="_blank" rel="nofollow noopener noreferrer">here</a> and what it does is <strong>generate factorials of numbers at compile time. </strong>So when you run it it comes back with the answer immediately.</p>
<pre><code class="language-cpp" data-line="">// factorial.cpp

#include &lt;iostream&gt;

template &lt;int N&gt;                                                                 // (2)
struct Factorial{
    static int const value = N * Factorial&lt;N-1&gt;::value;
};

template &lt;&gt;                                                                      // (3)
struct Factorial&lt;1&gt;{
    static int const value = 1;
};

int main(){
    
    std::cout &lt;&lt; std::endl;
    
    std::cout &lt;&lt; &quot;Factorial&lt;5&gt;::value: &quot; &lt;&lt; Factorial&lt;5&gt;::value &lt;&lt; std::endl;    // (1)
    std::cout &lt;&lt; &quot;Factorial&lt;10&gt;::value: &quot; &lt;&lt; Factorial&lt;10&gt;::value &lt;&lt; std::endl;
    
    std::cout &lt;&lt; std::endl;

}</code></pre>
<p>But if you think that is clever how about a game where every time you compile it, it makes a move and remembers the move between turns? A developer called Matt Bierner has <a title="Link to C++ Compile time snake game on GitHub" href="https://github.com/mattbierner/STT-C-Compile-Time-Snake" target="_blank" rel="nofollow noopener noreferrer">developed a snake game</a> using template metaprogramming.</p>
<p>So I downloaded it into my Ubuntu, installed Clang and clang tool just for good measure and compiled. This is the output. Iv&#8217;e snipped a lot out after the first two. Ot&#8217;s very clever, in this case, not much use but I doubt if there is any other programming language in which you could do this. The compile plays the game, running it just outputs the results. Yes it&#8217;s not exactly practical but still&#8230;</p>
<pre>david@davidvm:~/STT-C-Compile-Time-Snake-master/stt-snake$ clang++ -std=c++1y main.cpp -o snake ; ./snake
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺&#x25b6;*╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺*╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺&#x25b6;&#x25b6;╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
....
-- You Are Dead --
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
*╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺▼╺╺╺╺╺╺╺╺
╺█▲╺╺╺╺╺╺╺
╺▼&#x25b6;╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
</pre>The post <a href="https://learncgames.com/c-template-metaprogramming-game/">C++ Template Metaprogramming Game</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1857</post-id>	</item>
	</channel>
</rss>
