<?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[Generischer Typ für Container mit begin() und end()?]]></title><description><![CDATA[<p>Moin,</p>
<p>gibt es eine möglichkeit einen Templateparameter so einzuschränken, dass er nur Typen mit begin() und end() Memberfuntktion akzeptiert?</p>
<pre><code>template&lt;typename T&gt;
void Klasse::pusht(T container)
{
	for(auto i: container)
	{
		push(i);
	}
}

// Jetzt sollte folgendes funktionieren:
std::vector&lt;int&gt; Vektor;
std::list&lt;int&gt; Liste;
Instanz.pusht(Vektor);

// Folgendes jedoch nicht:
int i = 1;
Instanz.pusht(i);
</code></pre>
<p>Prinzipiell funktioniert dies bereits wie es soll, aber die Fehlermeldungen beim Aufruf von pusht(int) werden der for-Schleife zugeordnet (offensichtlich, int hat ja keine begin-Methode). Schöner wäre es für mich, wenn ich einen generischen Typ für &quot;Klassen mit begin() und end()&quot; hätte. Gibt es soetwas?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/312280/generischer-typ-für-container-mit-begin-und-end</link><generator>RSS for Node</generator><lastBuildDate>Sun, 02 Aug 2026 23:08:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/312280.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 31 Dec 2012 10:20:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Generischer Typ für Container mit begin() und end()? on Mon, 31 Dec 2012 10:20:37 GMT]]></title><description><![CDATA[<p>Moin,</p>
<p>gibt es eine möglichkeit einen Templateparameter so einzuschränken, dass er nur Typen mit begin() und end() Memberfuntktion akzeptiert?</p>
<pre><code>template&lt;typename T&gt;
void Klasse::pusht(T container)
{
	for(auto i: container)
	{
		push(i);
	}
}

// Jetzt sollte folgendes funktionieren:
std::vector&lt;int&gt; Vektor;
std::list&lt;int&gt; Liste;
Instanz.pusht(Vektor);

// Folgendes jedoch nicht:
int i = 1;
Instanz.pusht(i);
</code></pre>
<p>Prinzipiell funktioniert dies bereits wie es soll, aber die Fehlermeldungen beim Aufruf von pusht(int) werden der for-Schleife zugeordnet (offensichtlich, int hat ja keine begin-Methode). Schöner wäre es für mich, wenn ich einen generischen Typ für &quot;Klassen mit begin() und end()&quot; hätte. Gibt es soetwas?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2284685</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2284685</guid><dc:creator><![CDATA[ntldr]]></dc:creator><pubDate>Mon, 31 Dec 2012 10:20:37 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Typ für Container mit begin() und end()? on Mon, 31 Dec 2012 10:26:25 GMT]]></title><description><![CDATA[<p>Nicht wirklich, vielleicht bekommt man das mit ganz viel Meta-Gefrickel hin, aber letztlich kannst du es auch gleich so lassen. (Das ist übrigens der Grund warum alle auf Concepts warten, .. ;))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2284686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2284686</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Mon, 31 Dec 2012 10:26:25 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Typ für Container mit begin() und end()? on Mon, 31 Dec 2012 11:54:50 GMT]]></title><description><![CDATA[<p>mit c++11 Unterstützung schmeist bei mir der compiler eine fehlermeldung, wenn der Type &quot;T&quot; kein begin() kennt:</p>
<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;
using namespace std;

template&lt;typename T&gt;
void pusht(T container)
{
    for(auto i: container)
    {
        std::cout&lt;&lt;i;
    }
}

int main()
{
    vector&lt;int&gt; intvec;
    intvec.push_back(12);

    pusht(intvec);

    int t = 5;

    pusht(t);
}
</code></pre>
<p>Fehlermeldung:</p>
<blockquote>
<p>../dffhfghfgh/test.cpp: In function 'void pusht(T) [with T = int]':<br />
../dffhfghfgh/test.cpp:24:12: instantiated from here<br />
../dffhfghfgh/test.cpp:8:5: error: no matching function for call to 'begin(int&amp;)'<br />
../dffhfghfgh/test.cpp:8:5: note: candidates are:<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/bits/range_access.h:87:28: note: template&lt;class _Tp, long unsigned int _Nm&gt; _Tp* std::begin(_Tp (&amp;)[_Nm])<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/bits/range_access.h:58:63: note: template&lt;class _Container&gt; decltype (__cont.begin()) std::begin(const _Container&amp;)<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/bits/range_access.h:48:5: note: template&lt;class _Container&gt; decltype (__cont.begin()) std::begin(_Container&amp;)<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/initializer_list:86:38: note: template&lt;class _Tp&gt; constexpr const _Tp* std::begin(std::initializer_list&lt;_Tp&gt;)<br />
../dffhfghfgh/test.cpp:8:5: error: no matching function for call to 'end(int&amp;)'<br />
../dffhfghfgh/test.cpp:8:5: note: candidates are:<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/bits/range_access.h:97:26: note: template&lt;class _Tp, long unsigned int _Nm&gt; _Tp* std::end(_Tp (&amp;)[_Nm])<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/bits/range_access.h:78:59: note: template&lt;class _Container&gt; decltype (__cont.end()) std::end(const _Container&amp;)<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/bits/range_access.h:68:5: note: template&lt;class _Container&gt; decltype (__cont.end()) std::end(_Container&amp;)<br />
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/initializer_list:96:36: note: template&lt;class _Tp&gt; constexpr const _Tp* std::end(std::initializer_list&lt;_Tp&gt;)<br />
../dffhfghfgh/test.cpp:8:5: error: unable to deduce 'auto' from '&lt;expression error&gt;'</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2284717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2284717</guid><dc:creator><![CDATA[firefly]]></dc:creator><pubDate>Mon, 31 Dec 2012 11:54:50 GMT</pubDate></item><item><title><![CDATA[Reply to Generischer Typ für Container mit begin() und end()? on Mon, 31 Dec 2012 13:40:13 GMT]]></title><description><![CDATA[<p>Man kann SFINAE ausnutzen (substitution failure is not an error). Z.B. so:</p>
<pre><code class="language-cpp">template&lt;class T, class...More&gt;
using First = T;

namespace foo {

  using std::begin;
  using std::end;

  template&lt;class C&gt;
  auto dings(C const&amp; c)
  -&gt; First&lt;void,decltype(begin(c),end(c))&gt;
  {
    for (auto&amp;&amp; elem : c) {
      :::
    }
  }

}
</code></pre>
<p>Falls man über c so nicht iterieren kann, sind die Ausdrucke begin(c) und end(c) nicht gültig, was dazu führt, dass der Compiler den return-Typ nicht feststellen kann, was dazu führt, dass die die Deduktion von C fehlschlägt, was dazu führt, dass das Template ignoriert wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2284759</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2284759</guid><dc:creator><![CDATA[kk-in-hh]]></dc:creator><pubDate>Mon, 31 Dec 2012 13:40:13 GMT</pubDate></item></channel></rss>