<?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[template parameter container]]></title><description><![CDATA[<p>Hallo zusammen,<br />
Ich geb mal etwas Code um mein Problem auf zu zeigen:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;

#include &lt;boost/bind.hpp&gt;

class SimpleClass
{
private:
	int m_Value1 ;
	int m_Value2 ;
public:
	SimpleClass(int value1,int value2)
	:m_Value1(value1),m_Value2(value2)
	{}
	bool isValue(int x) const
	{
		return x==m_Value1 ;
	}
};

template &lt;template &lt;typename&gt; class container_type&gt;
void do_something(typename container_type&lt;SimpleClass&gt;::iterator begin,typename container_type&lt;SimpleClass&gt;::iterator end) 
{
	std::find_if(begin,end,boost::bind(&amp;SimpleClass::isValue,_1,100)) ;		
}

int main()
{
	std::vector&lt;SimpleClass&gt; data ;
	data.push_back(SimpleClass(10,29)) ;
	data.push_back(SimpleClass(100,28)) ;
	data.push_back(SimpleClass(11,22)) ;
	data.push_back(SimpleClass(13,21)) ;

	do_something(data.begin(),data.end()) ;
}
</code></pre>
<p>Der GCC spuckt folgende Fehlermeldung:</p>
<p>container_as_template.cpp: In function 'int main()':<br />
container_as_template.cpp:37:38: error: no matching function for call to 'do_something(std::vector&lt;SimpleClass&gt;::iterator, std::vector&lt;SimpleClass&gt;::iterator)'<br />
do_something(data.begin(),data.end()) ;<br />
^<br />
container_as_template.cpp:37:38: note: candidate is:<br />
container_as_template.cpp:23:6: note: template&lt;template&lt;class&gt; class container_type&gt; void do_something(typename container_type&lt;SimpleClass&gt;::const_iterator, typename container_type&lt;SimpleClass&gt;::const_iterator)<br />
void do_something(typename container_type&lt;SimpleClass&gt;::const_iterator begin,typename container_type&lt;SimpleClass&gt;::const_iterator end)<br />
^<br />
container_as_template.cpp:23:6: note: template argument deduction/substitution failed:<br />
container_as_template.cpp:37:38: note: couldn't deduce template parameter 'template&lt;class&gt; class container_type'<br />
do_something(data.begin(),data.end()) ;</p>
<p>Wie müssen bei do_something die template parameter angegeben werden, dass der Compiler das frisst?<br />
Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/332749/template-parameter-container</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 15:12:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/332749.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 20 May 2015 08:11:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to template parameter container on Wed, 20 May 2015 08:11:12 GMT]]></title><description><![CDATA[<p>Hallo zusammen,<br />
Ich geb mal etwas Code um mein Problem auf zu zeigen:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;vector&gt;

#include &lt;boost/bind.hpp&gt;

class SimpleClass
{
private:
	int m_Value1 ;
	int m_Value2 ;
public:
	SimpleClass(int value1,int value2)
	:m_Value1(value1),m_Value2(value2)
	{}
	bool isValue(int x) const
	{
		return x==m_Value1 ;
	}
};

template &lt;template &lt;typename&gt; class container_type&gt;
void do_something(typename container_type&lt;SimpleClass&gt;::iterator begin,typename container_type&lt;SimpleClass&gt;::iterator end) 
{
	std::find_if(begin,end,boost::bind(&amp;SimpleClass::isValue,_1,100)) ;		
}

int main()
{
	std::vector&lt;SimpleClass&gt; data ;
	data.push_back(SimpleClass(10,29)) ;
	data.push_back(SimpleClass(100,28)) ;
	data.push_back(SimpleClass(11,22)) ;
	data.push_back(SimpleClass(13,21)) ;

	do_something(data.begin(),data.end()) ;
}
</code></pre>
<p>Der GCC spuckt folgende Fehlermeldung:</p>
<p>container_as_template.cpp: In function 'int main()':<br />
container_as_template.cpp:37:38: error: no matching function for call to 'do_something(std::vector&lt;SimpleClass&gt;::iterator, std::vector&lt;SimpleClass&gt;::iterator)'<br />
do_something(data.begin(),data.end()) ;<br />
^<br />
container_as_template.cpp:37:38: note: candidate is:<br />
container_as_template.cpp:23:6: note: template&lt;template&lt;class&gt; class container_type&gt; void do_something(typename container_type&lt;SimpleClass&gt;::const_iterator, typename container_type&lt;SimpleClass&gt;::const_iterator)<br />
void do_something(typename container_type&lt;SimpleClass&gt;::const_iterator begin,typename container_type&lt;SimpleClass&gt;::const_iterator end)<br />
^<br />
container_as_template.cpp:23:6: note: template argument deduction/substitution failed:<br />
container_as_template.cpp:37:38: note: couldn't deduce template parameter 'template&lt;class&gt; class container_type'<br />
do_something(data.begin(),data.end()) ;</p>
<p>Wie müssen bei do_something die template parameter angegeben werden, dass der Compiler das frisst?<br />
Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454160</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454160</guid><dc:creator><![CDATA[case]]></dc:creator><pubDate>Wed, 20 May 2015 08:11:12 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Wed, 20 May 2015 08:23:05 GMT]]></title><description><![CDATA[<p>Du musst bei do_something explizit den ContainerTypen angeben, da der Compiler ihn nicht anhand des Iterators deduzieren kann.<br />
Wenn du nur Container auf deinen Typen zulassen will, mach den Iterator direkt als Templateparameter und nutz enable_if oder static_assert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454164</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454164</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Wed, 20 May 2015 08:23:05 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Wed, 20 May 2015 16:35:03 GMT]]></title><description><![CDATA[<p>es müsste eigentlich</p>
<pre><code>template &lt;typename, typename = std::allocator&lt;SimpleClass&gt;&gt;
</code></pre>
<p>heißen oder ähnlich (ähnlich heißt, dass man es dann explizit angibt später, anstatt als default argument, oder als weiterer template parameter, ...).<br />
STL container haben 2 template params.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454227</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454227</guid><dc:creator><![CDATA[5cript]]></dc:creator><pubDate>Wed, 20 May 2015 16:35:03 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Thu, 21 May 2015 07:19:36 GMT]]></title><description><![CDATA[<p>Es gibt auch Container mit 3 oder 4 Parametern.<br />
Ich denke es ist besser es so zu machen wie Nathan vorgeschlagen hat und die Iteratoren als Templateparameter zu nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454282</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454282</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 21 May 2015 07:19:36 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Thu, 21 May 2015 07:27:38 GMT]]></title><description><![CDATA[<p>Danke an euch allen, hab das Problem nach Nathan Vorschlag mit static_assert gelöst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454287</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454287</guid><dc:creator><![CDATA[case]]></dc:creator><pubDate>Thu, 21 May 2015 07:27:38 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Thu, 21 May 2015 19:33:45 GMT]]></title><description><![CDATA[<p>Gibt's irgendeinen triftigen Grund, nicht <code>template&lt;typename Iterator&gt; ...</code> zu nehmen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454351</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454351</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 21 May 2015 19:33:45 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Thu, 21 May 2015 19:41:11 GMT]]></title><description><![CDATA[<blockquote>
<p>Wenn du nur Container auf deinen Typen zulassen will, <strong>mach den Iterator direkt als Templateparameter und nutz enable_if oder static_assert</strong>.</p>
</blockquote>
<p>case schrieb:</p>
<blockquote>
<p>Danke an euch allen, <strong>hab das Problem nach Nathan Vorschlag mit static_assert gelöst.</strong></p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2454353</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454353</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Thu, 21 May 2015 19:41:11 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Thu, 21 May 2015 19:48:42 GMT]]></title><description><![CDATA[<p>Ja, das habe ich wohl auch gelesen, ich meine eher den Teil, den du nicht fett markiert hast:</p>
<blockquote>
<p><strong>Wenn du nur Container auf deinen Typen zulassen will,</strong></p>
</blockquote>
<p>Warum? So hat man mehr Aufwand für Code, der weniger kann und den Anwender mehr ärgert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454356</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454356</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 21 May 2015 19:48:42 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Thu, 21 May 2015 20:11:55 GMT]]></title><description><![CDATA[<blockquote>
<p>Ja, das habe ich wohl auch gelesen, ich meine eher den Teil, den du nicht fett markiert hast:</p>
</blockquote>
<p>Danach hast du doch nicht gefragt? Der TE hat jetzt wahrscheinlich</p>
<pre><code>template &lt;typename Iter&gt;
void do_something(Iter begin, Iter end) 
{
    static_assert( std::is_base_of&lt;SimpleClass, typename std::iterator_traits&lt;Iter&gt;::value_type&gt;{}, &quot;&quot; );
    std::find_if(begin,end,boost::bind(&amp;SimpleClass::isValue,_1,100)) ;     
}
</code></pre>
<p>stehen, was der Form entspricht die du erwähnt hast.</p>
<blockquote>
<p>So hat man mehr Aufwand für Code, der weniger kann und den Anwender mehr ärgert.</p>
</blockquote>
<p>Oder man hat Code der dem Anwender ordentliche Diagnostik mitgibt. Vielleicht hätte er auch <code>enable_if</code> +Kommentar nehmen können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454359</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454359</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Thu, 21 May 2015 20:11:55 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Thu, 21 May 2015 20:24:02 GMT]]></title><description><![CDATA[<p>Braunstein schrieb:</p>
<blockquote>
<p>Es gibt auch Container mit 3 oder 4 Parametern.<br />
Ich denke es ist besser es so zu machen wie Nathan vorgeschlagen hat und die Iteratoren als Templateparameter zu nehmen.</p>
</blockquote>
<p>Wer <em>versucht</em> über stacks zu iterieren?<br />
Spaß bei Seite: Ich hatte noch überlegt - editiere ich mein Beitrag noch um auf Nathan zu verweisen?</p>
<p>Oh und bitte erwähne jetzt nicht die Map oder selbstimplementierte Spezielcontainer -.-</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454360</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454360</guid><dc:creator><![CDATA[5cript]]></dc:creator><pubDate>Thu, 21 May 2015 20:24:02 GMT</pubDate></item><item><title><![CDATA[Reply to template parameter container on Fri, 22 May 2015 04:41:49 GMT]]></title><description><![CDATA[<p>5cript schrieb:</p>
<blockquote>
<p>Wer versucht über stacks zu iterieren?<br />
Spaß bei Seite: Ich hatte noch überlegt - editiere ich mein Beitrag noch um auf Nathan zu verweisen?</p>
<p>Oh und bitte erwähne jetzt nicht die Map oder selbstimplementierte Spezielcontainer -.-</p>
</blockquote>
<p>Nö, ich erwähne set. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454385</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454385</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Fri, 22 May 2015 04:41:49 GMT</pubDate></item></channel></rss>