<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Smart Pointer Casting ( Base to Derived&amp;lt;T&amp;gt; )]]></title><description><![CDATA[<p>Can someone explain the following to me ? :</p>
<pre><code>class Base
{
fn_base(){...};
};

template &lt; class T &gt; 
class Derived : public Base
{
fn_derived(){...};
T value_;
};
</code></pre>
<p>I have 2 classes as you can see above.<br />
Additionally I have a map that cointains shared_ptr&lt;Base&gt; + a specific id.</p>
<pre><code>std::map&lt; const std::string id, std::shared_ptr&lt;Base&gt;&gt; objects_;
</code></pre>
<p>Now I want to store an object of type Derived&lt;T&gt; in the map ( works fine ) and receive the value of it somewhere else ( here is the problem )</p>
<pre><code>template &lt; class T &gt;
T getObjectValue( const std::string id ){
    if ( object_.find(id) != objects_end()){
        return std::dynamic_pointer_cast&lt;Derived&lt;T&gt;&gt;(objects_.at(id))-&gt;getValue();
    }
}
</code></pre>
<p>This :</p>
<pre><code>return std::dynamic_pointer_cast&lt;Derived&lt;T&gt;&gt;(objects_.at(id))-&gt;getValue();
</code></pre>
<p>Doesn't work. But when I replace T with a specified type it works ?</p>
<pre><code>return std::dynamic_pointer_cast&lt;Derived&lt;int&gt;&gt;(objects_.at(id))-&gt;getValue();
</code></pre>
<p>Can someone explain that ? And if you can, would you mind giving me a hint on how I can work around that ?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/336865/smart-pointer-casting-base-to-derived-lt-t-gt</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 20:35:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/336865.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 21 Feb 2016 15:13:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Smart Pointer Casting ( Base to Derived&amp;lt;T&amp;gt; ) on Sun, 21 Feb 2016 15:13:59 GMT]]></title><description><![CDATA[<p>Can someone explain the following to me ? :</p>
<pre><code>class Base
{
fn_base(){...};
};

template &lt; class T &gt; 
class Derived : public Base
{
fn_derived(){...};
T value_;
};
</code></pre>
<p>I have 2 classes as you can see above.<br />
Additionally I have a map that cointains shared_ptr&lt;Base&gt; + a specific id.</p>
<pre><code>std::map&lt; const std::string id, std::shared_ptr&lt;Base&gt;&gt; objects_;
</code></pre>
<p>Now I want to store an object of type Derived&lt;T&gt; in the map ( works fine ) and receive the value of it somewhere else ( here is the problem )</p>
<pre><code>template &lt; class T &gt;
T getObjectValue( const std::string id ){
    if ( object_.find(id) != objects_end()){
        return std::dynamic_pointer_cast&lt;Derived&lt;T&gt;&gt;(objects_.at(id))-&gt;getValue();
    }
}
</code></pre>
<p>This :</p>
<pre><code>return std::dynamic_pointer_cast&lt;Derived&lt;T&gt;&gt;(objects_.at(id))-&gt;getValue();
</code></pre>
<p>Doesn't work. But when I replace T with a specified type it works ?</p>
<pre><code>return std::dynamic_pointer_cast&lt;Derived&lt;int&gt;&gt;(objects_.at(id))-&gt;getValue();
</code></pre>
<p>Can someone explain that ? And if you can, would you mind giving me a hint on how I can work around that ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488069</guid><dc:creator><![CDATA[xxXLukas97Xxx]]></dc:creator><pubDate>Sun, 21 Feb 2016 15:13:59 GMT</pubDate></item><item><title><![CDATA[Reply to Smart Pointer Casting ( Base to Derived&amp;lt;T&amp;gt; ) on Sun, 21 Feb 2016 15:20:35 GMT]]></title><description><![CDATA[<blockquote>
<p>Now I want to store an object of type Derived&lt;T&gt; in the map ( works fine )[...]</p>
</blockquote>
<p>I meant a std::shared_ptr&lt;Derived&lt;T&gt;&gt;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488070</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488070</guid><dc:creator><![CDATA[xxXLukas97Xxx]]></dc:creator><pubDate>Sun, 21 Feb 2016 15:20:35 GMT</pubDate></item><item><title><![CDATA[Reply to Smart Pointer Casting ( Base to Derived&amp;lt;T&amp;gt; ) on Sun, 21 Feb 2016 15:36:52 GMT]]></title><description><![CDATA[<p>Why is your signature in German but the post in English?</p>
<p>Please show us a reproducible example, don't merely paste extracts that don't even compile.<br />
My guess (as you didn't show more code, which would be required for a proper assessment) is that the actual code - not the one you just typed into the post - contains a <em>template-id</em> for <code>getValue</code> :</p>
<pre><code>template &lt; class T &gt;
T getObjectValue( const std::string id ){
    if ( object_.find(id) != objects_end()){
        return std::dynamic_pointer_cast&lt;Derived&lt;T&gt;&gt;(objects_.at(id))-&gt;getValue&lt;...&gt;();
    }
}
</code></pre>
<p>Instead, try</p>
<pre><code>template &lt; class T &gt;
T getObjectValue( const std::string id ){
    if ( object_.find(id) != objects_end()){
        return std::dynamic_pointer_cast&lt;Derived&lt;T&gt;&gt;(objects_.at(id))-&gt;template getValue&lt;...&gt;();
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2488076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488076</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sun, 21 Feb 2016 15:36:52 GMT</pubDate></item></channel></rss>