<?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>TiMoch &#187; visual studio</title>
	<atom:link href="https://timoch.com/blog/tag/visual-studio/feed/" rel="self" type="application/rss+xml" />
	<link>https://timoch.com/blog</link>
	<description>on edge</description>
	<lastBuildDate>Tue, 29 Apr 2014 15:02:50 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>How to get confused by VC++</title>
		<link>https://timoch.com/blog/2013/03/how-to-get-confused-by-vc/</link>
		<comments>https://timoch.com/blog/2013/03/how-to-get-confused-by-vc/#comments</comments>
		<pubDate>Wed, 27 Mar 2013 14:17:34 +0000</pubDate>
		<dc:creator><![CDATA[timoch]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[c++0x]]></category>
		<category><![CDATA[c++11]]></category>
		<category><![CDATA[vc++]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.timoch.com/blog/?p=131</guid>
		<description><![CDATA[Today, I came across a confusing compilation error with MS VC++ using a std::unique_ptr with a custom deleter. Here the code snippet: [crayon-69d57c2431ad2945145044/] The compiler complains : 12345678910111213c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2218): error C2338: unique_ptr constructed with null deleter pointer 1&#62; c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2214) : while compiling class template member function [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Today, I came across a confusing compilation error with MS VC++ using a <em>std::unique_ptr</em> with a custom deleter.</p>
<p>Here the code snippet:</p>
<p></p><pre class="crayon-plain-tag">#include &lt;memory&gt;
#include &lt;string&gt;

typedef void (*void_delete)(void *);

template&lt;typename T&gt;
void void_deleter(void * ptr) { delete reinterpret_cast&lt;T*&gt;(ptr); }

std::unique_ptr&lt;void, void_delete&gt; f1() {
return std::unique_ptr&lt;void, void_delete&gt;(new std::string("haha"), void_deleter);
}
std::unique_ptr&lt;void, void_delete&gt; f2() {
return std::unique_ptr&lt;void, void_delete&gt;(nullptr);
}</pre><p> </p>
<p>The compiler complains :</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2218): error C2338: unique_ptr constructed with null deleter pointer<br />
1&gt; c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2214) : while compiling class template member function 'std::unique_ptr::unique_ptr(std::nullptr_t)'<br />
1&gt; with<br />
1&gt; [<br />
1&gt; _Ty=void,<br />
1&gt; _Dx=void_delete<br />
1&gt; ]<br />
1&gt; c:\sources\source1.cpp(9) : see reference to class template instantiation 'std::unique_ptr' being compiled<br />
1&gt; with<br />
1&gt; [<br />
1&gt; _Ty=void,<br />
1&gt; _Dx=void_delete<br />
1&gt; ]</div></td></tr></tbody></table></div>
<p>The gotcha is that the compiler complains of a <em>reference to class template instantiation</em> on line 9. This is the definition of f1(). </p>
<p>However, the actual issue is on line 13 in the implementation of f2(). A <em>std::unique_ptr</em> is instantiated with a nullptr and no deleter. The nullptr constructor has a <em>static_assert</em> that protects from uninitialized function-pointer deleter. I wonder what could go wrong with an uninitialized function pointer &#8230; <img src="https://timoch.com/blog/wp-includes/images/smilies/icon_neutral.gif" alt=":-|" class="wp-smiley" /> </p>
<p>The compiler gives you the line of the first appearance of <em>std::unique_ptr</em> in the compilation unit not the actual line that causes the error &#8230; Easy enough to spot on a trimmed down example like this one. It tooks me quite some time to find in a code file with several hundred lines of code and many changes at once.</p>
]]></content:encoded>
			<wfw:commentRss>https://timoch.com/blog/2013/03/how-to-get-confused-by-vc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
