<?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>Mindaugas Rupšys &#187; Dev</title>
	<atom:link href="http://mindaugasrupsys.com/category/my-dev-corner/feed" rel="self" type="application/rss+xml" />
	<link>http://mindaugasrupsys.com</link>
	<description>homepage, blog, portfolio, stuff</description>
	<lastBuildDate>Mon, 24 Oct 2011 11:23:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>C++ fun with typedef and const</title>
		<link>http://mindaugasrupsys.com/c-fun-with-typedef-and-const</link>
		<comments>http://mindaugasrupsys.com/c-fun-with-typedef-and-const#comments</comments>
		<pubDate>Wed, 03 Mar 2010 22:48:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Dev]]></category>

		<guid isPermaLink="false">http://mindaugasrupsys.com/?p=77</guid>
		<description><![CDATA[Weird isn't it? From first glance everything looks alright and I really didn't understood why it was not working. After some googling I find out what is going on. Problem is that qualifier (const or others) applies at the very top level, in my situation once I typedef type with pointer qualifier can not be injected between type and a pointer. ]]></description>
			<content:encoded><![CDATA[<p>I stumbled on some interesting problem today:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>MY_KEY<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> foo <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MY_KEY key<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #666666;">// ...</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>str <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;some str&quot;</span><span style="color: #008080;">;</span>
foo <span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// error: smth like unable to convert LPCSTR (const char*) to char*</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Weird isn&#8217;t it? From first glance everything looks alright and I really didn&#8217;t understood why it was not working. After some googling I find out what is going on. Problem is that qualifier (<strong>const</strong> or others) applies at the very top level, in my situation once I <strong>typedef</strong> type with pointer qualifier can not be injected between type and a pointer. Here what is happening:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>MY_KEY<span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> foo <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MY_KEY key<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> equals to <span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #0000ff;">void</span> foo <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> key<span style="color: #008000;">&#41;</span></pre></div></div>

<p>Now to avoid this situation <strong>typedef</strong> should be done without pointer, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">char</span> MY_KEY<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> foo <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> MY_KEY <span style="color: #000040;">*</span>key<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #666666;">// ...</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main <span style="color: #008000;">&#40;</span>...<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>str <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;some str&quot;</span><span style="color: #008080;">;</span>
foo <span style="color: #008000;">&#40;</span>str<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// and it works as it should</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mindaugasrupsys.com/c-fun-with-typedef-and-const/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

