{"id":194,"date":"2008-11-09T04:12:10","date_gmt":"2008-11-09T08:12:10","guid":{"rendered":"http:\/\/www.virtualroadside.com\/blog\/?p=194"},"modified":"2008-11-09T04:15:13","modified_gmt":"2008-11-09T08:15:13","slug":"c-template-fu-delete_if","status":"publish","type":"post","link":"https:\/\/www.virtualroadside.com\/blog\/index.php\/2008\/11\/09\/c-template-fu-delete_if\/","title":{"rendered":"C++ template-fu: delete_if"},"content":{"rendered":"<p>I&#8217;ve been doing a lot of work with templates lately, for my R* tree implementation (which is morphing into a R* tree and a B tree library) and some things that I&#8217;ve been playing around with for Roadnav, especially in regards to serialization.<\/p>\n<p>Heres a neat snippet that I created:<\/p>\n<pre>template &lt;bool&gt;\r\nstruct delete_if {\r\n\ttemplate &lt;typename T&gt;\r\n\tstatic void del(T t) { delete t; }\r\n};\r\n\r\ntemplate &lt;&gt;\r\nstruct delete_if\r\n{\r\n\ttemplate &lt;typename T&gt;\r\n\tstatic void del(T t) { \/* no-op, doesn't delete *\/ }\r\n};<\/pre>\n<p>This uses a partial specialization to do the decision portion, and a static member template to eliminate some of the typing (you could get rid of the member template, and you would end up calling delete_if&lt;type, delete_item&gt;::del ).<\/p>\n<p>So what this is intended for is those cases where you are implementing some generic container, and you don&#8217;t know whether the type you have is a pointer or not. You *could* use a normal bool as a parameter:<\/p>\n<pre>if (delete_it) delete var;<\/pre>\n<p>But primary problem with this is that it won&#8217;t compile if the type isn&#8217;t a pointer. So a sample of something similar to what I&#8217;m using it for is something like this:<\/p>\n<pre>template &lt; typename T, bool delete_item &gt;\r\nclass test {\r\npublic:\r\n\r\n\t~test()\r\n\t{\r\n\t\t\/\/ only release the item if the client specifies\r\n\t\t\/\/ that they want to\r\n\t\tdelete_if&lt;delete_item&gt;::del(val);\r\n\t}\r\n\r\nprivate:\r\n\tT val;\r\n};<\/pre>\n<p>Obviously this is just a really simple example of how you can use this. One could also use boost::is_pointer to detect whether the type is a pointer, and pass *that* to delete_if while using a &#8216;normal&#8217; variable to determine whether the client wants to have the item be deleted or not. I leave that as an exercise for the reader.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing a lot of work with templates lately, for my R* tree implementation (which is morphing into a R* tree and a B tree library) and some things that I&#8217;ve been playing around with for Roadnav, especially in regards to serialization. Heres a neat snippet that I created: template &lt;bool&gt; struct delete_if { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40,31],"tags":[],"_links":{"self":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/194"}],"collection":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=194"}],"version-history":[{"count":3,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/194\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}