<?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[Compiler Error bei Function Template mit VS2010 SP1]]></title><description><![CDATA[<p>Hallo,</p>
<p>Das untenstehende Bsp. erzeugt den markierten Compiler Error (getestet mit VS2010SP1). Dieselbe Semantik mit einem for-Loop und ohne Lambda kompiliert ohne Probleme.</p>
<p>- Ist der Compiler Error berechtigt? <strong>[geklärt, siehe unten]</strong><br />
- Ist es korrekt das ADL bei begin(..) und end(..) greifen sollte? <strong>[geklärt]</strong><br />
- Sind noch 2 typenames nötig bei source_container_type::value_type bzw. target_container_type::value_type? <strong>[geklärt, editiert]</strong></p>
<pre><code class="language-cpp">#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

template &lt;typename source_container_type, typename target_container_type&gt;
void copy_all(const source_container_type&amp; source, target_container_type&amp; target)
{
    std::for_each(begin(source), end(source), [&amp;] (const typename source_container_type::value_type&amp; element)
    {
        // error C2899: typename cannot be used outside a template declaration
        // error C2653: 'target_container_type' : is not a class or namespace name
        target.push_back(typename target_container_type::value_type(element));
    });
}

int main()
{
    std::vector&lt;int&gt; a;
    a.push_back(23);
    a.push_back(24);
    a.push_back(25);

    std::list&lt;int&gt; b;
    copy_all(a, b);
}
</code></pre>
<p>Vielen Dank<br />
Theta</p>
<p><strong>Antwort</strong><br />
Der Compiler Error ist ein Bug im VS2010 SP1:<br />
MS Connect: <a href="http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressions" rel="nofollow">http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressions</a><br />
StackOverflow Diskussion: <a href="http://stackoverflow.com/questions/9466338/compiler-error-in-function-template-with-vs2010-sp1" rel="nofollow">http://stackoverflow.com/questions/9466338/compiler-error-in-function-template-with-vs2010-sp1</a></p>
<p><strong>[edit, 0] typenames im Bsp. Code ergänzt</strong><br />
<strong>[edit, 1] 1. typename im Bsp. Code korrigiert</strong><br />
<strong>[edit, 2] Nach den ersten 2 Edits tritt zusätzlich noch Error C2899 auf.</strong><br />
<strong>[edit, 3] Frage bezüglich ADL als geklärt markiert.</strong><br />
<strong>[edit, 4] Frage bezüglich Compiler Error als geklärt markiert. Siehe Antwort oben.</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/300100/compiler-error-bei-function-template-mit-vs2010-sp1</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 22:51:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/300100.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 25 Feb 2012 11:45:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Compiler Error bei Function Template mit VS2010 SP1 on Mon, 27 Feb 2012 14:38:20 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Das untenstehende Bsp. erzeugt den markierten Compiler Error (getestet mit VS2010SP1). Dieselbe Semantik mit einem for-Loop und ohne Lambda kompiliert ohne Probleme.</p>
<p>- Ist der Compiler Error berechtigt? <strong>[geklärt, siehe unten]</strong><br />
- Ist es korrekt das ADL bei begin(..) und end(..) greifen sollte? <strong>[geklärt]</strong><br />
- Sind noch 2 typenames nötig bei source_container_type::value_type bzw. target_container_type::value_type? <strong>[geklärt, editiert]</strong></p>
<pre><code class="language-cpp">#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

template &lt;typename source_container_type, typename target_container_type&gt;
void copy_all(const source_container_type&amp; source, target_container_type&amp; target)
{
    std::for_each(begin(source), end(source), [&amp;] (const typename source_container_type::value_type&amp; element)
    {
        // error C2899: typename cannot be used outside a template declaration
        // error C2653: 'target_container_type' : is not a class or namespace name
        target.push_back(typename target_container_type::value_type(element));
    });
}

int main()
{
    std::vector&lt;int&gt; a;
    a.push_back(23);
    a.push_back(24);
    a.push_back(25);

    std::list&lt;int&gt; b;
    copy_all(a, b);
}
</code></pre>
<p>Vielen Dank<br />
Theta</p>
<p><strong>Antwort</strong><br />
Der Compiler Error ist ein Bug im VS2010 SP1:<br />
MS Connect: <a href="http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressions" rel="nofollow">http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressions</a><br />
StackOverflow Diskussion: <a href="http://stackoverflow.com/questions/9466338/compiler-error-in-function-template-with-vs2010-sp1" rel="nofollow">http://stackoverflow.com/questions/9466338/compiler-error-in-function-template-with-vs2010-sp1</a></p>
<p><strong>[edit, 0] typenames im Bsp. Code ergänzt</strong><br />
<strong>[edit, 1] 1. typename im Bsp. Code korrigiert</strong><br />
<strong>[edit, 2] Nach den ersten 2 Edits tritt zusätzlich noch Error C2899 auf.</strong><br />
<strong>[edit, 3] Frage bezüglich ADL als geklärt markiert.</strong><br />
<strong>[edit, 4] Frage bezüglich Compiler Error als geklärt markiert. Siehe Antwort oben.</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185546</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185546</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Mon, 27 Feb 2012 14:38:20 GMT</pubDate></item><item><title><![CDATA[Reply to Compiler Error bei Function Template mit VS2010 SP1 on Sat, 25 Feb 2012 11:58:05 GMT]]></title><description><![CDATA[<p>Ja, es fehlen die typenames. Ja, ADL greift hier. Aber warum nicht einfach so?</p>
<pre><code class="language-cpp">std::copy(begin(src), end(src), std::back_inserter(dest));
</code></pre>
<p>Oder:</p>
<pre><code class="language-cpp">std::list&lt;int&gt; dest(begin(src), end(src));
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2185553</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185553</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sat, 25 Feb 2012 11:58:05 GMT</pubDate></item><item><title><![CDATA[Reply to Compiler Error bei Function Template mit VS2010 SP1 on Sat, 25 Feb 2012 12:04:17 GMT]]></title><description><![CDATA[<blockquote>
<p>Aber warum nicht einfach so? ...</p>
</blockquote>
<p>Der Code ist ein Bsp. und es geht um das Prinzip.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185559</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185559</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Sat, 25 Feb 2012 12:04:17 GMT</pubDate></item><item><title><![CDATA[Reply to Compiler Error bei Function Template mit VS2010 SP1 on Sat, 25 Feb 2012 12:09:27 GMT]]></title><description><![CDATA[<p>Warum zeigst du Code, den du so gar nicht verwendest?</p>
<p>Anyway, dein typename ist falsch gesetzt. Richtiger wäre</p>
<pre><code class="language-cpp">const typename source_container_type::value_type&amp;
</code></pre>
<p>, ganz korrekt ist:</p>
<pre><code class="language-cpp">typename source_container_type::const_reference
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2185561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185561</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sat, 25 Feb 2012 12:09:27 GMT</pubDate></item><item><title><![CDATA[Reply to Compiler Error bei Function Template mit VS2010 SP1 on Sat, 25 Feb 2012 23:39:02 GMT]]></title><description><![CDATA[<p>Hat jmd. ev. noch eine Erklärung zum oben erwähnten Compiler Error?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185846</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185846</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Sat, 25 Feb 2012 23:39:02 GMT</pubDate></item><item><title><![CDATA[Reply to Compiler Error bei Function Template mit VS2010 SP1 on Sat, 25 Feb 2012 23:52:48 GMT]]></title><description><![CDATA[<p>theta schrieb:</p>
<blockquote>
<p>Hat jmd. ev. noch eine Erklärung zum oben erwähnten Compiler Error?</p>
</blockquote>
<p>Ne, aber das geht:</p>
<pre><code class="language-cpp">template &lt;typename source_container_type, typename target_container_type&gt;
void copy_all(const source_container_type&amp; source, target_container_type&amp; target)
{
    typedef typename target_container_type::value_type vtype;
    std::for_each(begin(source), end(source), [&amp;] (const vtype&amp; element)
    {
        target.push_back(vtype(element));
    });
}
</code></pre>
<p>Edit: Warum auch immer man hier for_each nutzen möchte. Ich muss leider sagen, dass ich die alte for Schleife hier noch besser finde. (Im Gegensatz zu Sutter scheinbar.., na ja, wenn die ranged-based for-loops endlich überall verfügbar sind, wird die eh jeder nutzen.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185847</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sat, 25 Feb 2012 23:52:48 GMT</pubDate></item><item><title><![CDATA[Reply to Compiler Error bei Function Template mit VS2010 SP1 on Mon, 27 Feb 2012 14:38:57 GMT]]></title><description><![CDATA[<p>Ok, danke. Ich muss vielleicht meine Frage präzisieren:<br />
Meiner Meinung nach sollte mein Code ohne Probleme kompilieren - die Frage ist ob er das gemäss Standard C++11 wirklich sollte?</p>
<p><strong>Die Antwort bezüglich Compiler Error ist in der Frage eingepflegt.</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2185849</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2185849</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Mon, 27 Feb 2012 14:38:57 GMT</pubDate></item></channel></rss>