<?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[variadic template]]></title><description><![CDATA[<p>hallo.</p>
<p>ich hab mir (aus spaß - bevor wieder die standard antworten kommen) mal vorgenommen, std::thread nachzuprogrammieren.<br />
für windows reicht im prinzip vorerst: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx" rel="nofollow">CreateThread</a>.</p>
<p>nur finde ich keinen ansatz, die ganzen typinformationen wieder zu bekommen, nachdem ich den thread gestartet habe (in der dummy-fkt bin und den void* pointer habe, in der mein fkt-ptr samt parametern steht).</p>
<p>also das thread erstellen ist kein problem, nur eben die vom user angegebene fkt aufzurufen. und da ich den api-aufruf auch ganz gern im source-file hätte, fehlt mir schon der ansatz. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":/"
      alt="😕"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/307176/variadic-template</link><generator>RSS for Node</generator><lastBuildDate>Fri, 07 Aug 2026 18:05:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/307176.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Aug 2012 09:29:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 09:29:25 GMT]]></title><description><![CDATA[<p>hallo.</p>
<p>ich hab mir (aus spaß - bevor wieder die standard antworten kommen) mal vorgenommen, std::thread nachzuprogrammieren.<br />
für windows reicht im prinzip vorerst: <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx" rel="nofollow">CreateThread</a>.</p>
<p>nur finde ich keinen ansatz, die ganzen typinformationen wieder zu bekommen, nachdem ich den thread gestartet habe (in der dummy-fkt bin und den void* pointer habe, in der mein fkt-ptr samt parametern steht).</p>
<p>also das thread erstellen ist kein problem, nur eben die vom user angegebene fkt aufzurufen. und da ich den api-aufruf auch ganz gern im source-file hätte, fehlt mir schon der ansatz. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":/"
      alt="😕"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243251</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 19 Aug 2012 09:29:25 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 09:36:17 GMT]]></title><description><![CDATA[<p>Wer sagt, dass die dummy-fkt kein Template sein darf?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243253</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243253</guid><dc:creator><![CDATA[gnd]]></dc:creator><pubDate>Sun, 19 Aug 2012 09:36:17 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 10:37:05 GMT]]></title><description><![CDATA[<p>Mach doch sowas in der Richtung:</p>
<pre><code class="language-cpp">template&lt;typename Callable&gt;
DWORD WINAPI dummyFunc(LPVOID ptr)
{
        (*reinterpret_cast&lt;Callable*&gt;(ptr))();
        return 0;
}

struct thread
{
        template&lt;typename Callable, typename ... argTs&gt;
        thread(Callable&amp;&amp; callable, argTs&amp;&amp;... args)
        {
                auto bound = std::bind(std::forward&lt;Callable&gt;(callable), std::forward&lt;argTs&gt;(args)...);
                auto ptr = dummyFunc&lt;decltype(bound)&gt;;

                mHandle = CreateThread(nullptr, 0, ptr, &amp;bound, 0, nullptr);
                if( !mHandle )//opt. thread-id nehmen
                        throw ....;
        }

private:
        HANDLE WINAPI mHandle;
};
</code></pre>
<p>Evt. noch ein paar static assertions, damit die Fehlermeldungen nicht so kryptisch werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243265</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 19 Aug 2012 10:37:05 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:34:57 GMT]]></title><description><![CDATA[<p>Die gute alte Laufzeitpolymorphie ...</p>
<p>... also irgendwie so ...</p>
<pre><code class="language-cpp">class abstract_callable
{
public:
    virtual ~abstract_callable() {}
    virtual void run() = 0; ///&lt; MUST only be called ONCE!
};

template&lt;int...&gt;
struct int_pack {};

template&lt;class FunctorType, class TupleType, class Indices&gt;
class callable;

template&lt;class F, class...T, int...I&gt;
class callable&lt;F,std::tuple&lt;T...&gt;,int_pack&lt;I...&gt;&gt;
    : public abstract_callable
{
    F fun;
    std::tuple&lt;T...&gt; params;
public:
    template&lt;class A, class...Args&gt;
    explicit callable(A&amp;&amp; a, Args&amp;&amp;...args)
        : fun(std::forward&lt;A&gt;(a))
        , params(std::forward&lt;Args&gt;(args)...)
    {}

    void run()
    { fun(std::forward&lt;T&gt;(std::get&lt;I&gt;(params))...); }
};

template&lt;class F, class...Args&gt;
std::unique_ptr&lt;abstract_callable&gt; make_callable(F&amp;&amp; fun, Args&amp;&amp;...args)
{
    using std::forward;
    using std::unique_ptr;
    using std::make_tuple;
    typedef typename std::decay&lt;F&gt;::type fun_type;
    typedef decltype(make_tuple(forward&lt;Args&gt;(args)...)) tuple_type;
    typedef typename make_indices&lt;sizeof...(Args)&gt;::type indices_type;
    typedef callable&lt;fun_type,tuple_type,indices_type&gt; call_type;
    unique_ptr&lt;abstract_callable&gt; ac (
        new call_type(forward&lt;F&gt;(fun),forward&lt;Args&gt;(args)...) );
    return ac;
}

extern &quot;C&quot; void free_thread_func(void *user_data)
{
    std::unique_ptr&lt;abstract_callable&gt; ac (static_cast&lt;abstract_callable*&gt;(user_data));
    try {
        ac-&gt;run();
    } catch (...) {
        // do something sensible here
    }
}
</code></pre>
<p>(ungetestet)</p>
<p>Da wo der Thread tatsächlich gestartet wird muss man natürlich den rohen Zeiger aus dem von make_callable zurückgegebenen unique_ptr per release befreien, damit das Objekt nicht schon wieder zerstört wird, bevor der Thread überhaupt zum Zuge kommt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243276</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243276</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:34:57 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:00:08 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Mach doch sowas in der Richtung:</p>
<pre><code class="language-cpp">... std::bind ...
</code></pre>
</blockquote>
<p>Wenn du std::bind dafür verwendest, ist es dir nicht Möglich Objekte von move-only Typen an einen Thread &quot;per value&quot; zu übergeben, da bind die gebundenen Parameter als Lvalue übergibt; denn man könnte den Funktor ja mehrmals ausführen und beim zweiten Mal soll es immer noch &quot;funktionieren&quot;. std::thread fürht das Ding aber garantiert höchstens nur einmal aus. Es darf also gebundene Parameter als Rvalue übergeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243277</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243277</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:00:08 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:00:19 GMT]]></title><description><![CDATA[<blockquote>
<p>und da ich den api-aufruf auch ganz gern im source-file hätte, fehlt mir schon der ansatz. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":/"
      alt="😕"
    /></p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2243278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243278</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:00:19 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:02:35 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<blockquote>
<p>und da ich den api-aufruf auch ganz gern im source-file hätte, fehlt mir schon der ansatz. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":/"
      alt="😕"
    /></p>
</blockquote>
</blockquote>
<p>Der API-Aufruf findet doch aber in einem Funktionstemplate statt? Und das kannste ja nicht in Source&amp;Header aufteilen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243279</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243279</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:02:35 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:03:00 GMT]]></title><description><![CDATA[<p>Zu CreateThread mal ein kleines Zitat aus der MSDN:</p>
<blockquote>
<p>A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multithreaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2243280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243280</guid><dc:creator><![CDATA[DrakoXP]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:03:00 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:19:15 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p>Mach doch sowas in der Richtung:</p>
<pre><code class="language-cpp">... std::bind ...
</code></pre>
</blockquote>
<p>Wenn du std::bind dafür verwendest, ist es dir nicht Möglich Objekte von move-only Typen an einen Thread &quot;per value&quot; zu übergeben, da bind die gebundenen Parameter als Lvalue übergibt; denn man könnte den Funktor ja mehrmals ausführen und beim zweiten Mal soll es immer noch &quot;funktionieren&quot;. std::thread fürht das Ding aber garantiert höchstens nur einmal aus. Es darf also gebundene Parameter als Rvalue übergeben.</p>
</blockquote>
<p>Stimmt, fällt mir auf. Das hält seine Parameter in einem <code>std::tuple...</code><br />
Wie wärs mit einem Gefrickel mit <code>std::reference_wrapper</code> ? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
Edit: Nope. Temporäre Objekte können ja mit <code>std::reference_wrappe</code> r nicht gehalten werden, und sowieso...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243281</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243281</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:19:15 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:07:32 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<blockquote>
<p>und da ich den api-aufruf auch ganz gern im source-file hätte, fehlt mir schon der ansatz. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":/"
      alt="😕"
    /></p>
</blockquote>
</blockquote>
<p>Der API-Aufruf findet doch aber in einem Funktionstemplate statt? Und das kannste ja nicht in Source&amp;Header aufteilen...</p>
</blockquote>
<p>und weil ich denke, dass iwer nen vorschlag hat, wie es doch geht, habe ich hier gepostet...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243283</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:07:32 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:09:14 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>und weil ich denke, dass iwer nen vorschlag hat, wie es doch geht, habe ich hier gepostet...</p>
</blockquote>
<p>siehe meinen ersten Beitrag.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243284</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:09:14 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:15:12 GMT]]></title><description><![CDATA[<p>Warum baust du std::function nach, kk?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243288</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243288</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:15:12 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:15:49 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<p>und weil ich denke, dass iwer nen vorschlag hat, wie es doch geht, habe ich hier gepostet...</p>
</blockquote>
<p>siehe meinen ersten Beitrag.</p>
</blockquote>
<p>ahhh - ganz übersehen. danke : &gt;</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243289</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243289</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:15:49 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:30:06 GMT]]></title><description><![CDATA[<p>Kellerautomat schrieb:</p>
<blockquote>
<p>Warum baust du std::function nach, kk?</p>
</blockquote>
<p>Tue ich gar nicht. std::thread kannst du weder mit std::bind noch mit std::function implementieren -- jedenfalls nicht ohne größere Umwege. Der Standard verlangt, dass bei std::thread Rvalue-Parameter als solche weitergereicht werden.</p>
<p>Edit: Gut, man könnte std::function doch verwenden ... aber ich wüsste nicht, was das bringen sollte außer einer zusätzlichen Indirektion, auf die ich gern verzichten würde ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243292</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243292</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:30:06 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 11:46:29 GMT]]></title><description><![CDATA[<p>[quote=&quot;Sone&quot;]</p>
<pre><code class="language-cpp">template&lt;typename Callable&gt;
DWORD WINAPI dummyFunc(LPVOID ptr)
{
        (*reinterpret_cast&lt;Callable*&gt;(ptr))();
        return 0;
}

struct thread
{
        template&lt;typename Callable, typename ... argTs&gt;
        thread(Callable&amp;&amp; callable, argTs&amp;&amp;... args)
        {
                auto bound = std::bind(std::forward&lt;Callable&gt;(callable), std::forward&lt;argTs&gt;(args)...);
                auto ptr = dummyFunc&lt;decltype(bound)&gt;;

                mHandle = CreateThread(nullptr, 0, ptr, &amp;bound, 0, nullptr);
</code></pre>
<p>Oh, ich sehe gerade, dass du einen Zeiger auf ein automatisches Objekt übergibst. Das Objekt &quot;bound&quot; verschwindet doch aber gleich, wenn der Konstruktor fertig ist. 'Ne Freispeicherallozierung ist da schon praktischer. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243306</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243306</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 19 Aug 2012 11:46:29 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 14:43:16 GMT]]></title><description><![CDATA[<p>Edit: Gleich kommt ne gefixte Version.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243323</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243323</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sun, 19 Aug 2012 14:43:16 GMT</pubDate></item><item><title><![CDATA[Reply to variadic template on Sun, 19 Aug 2012 14:42:12 GMT]]></title><description><![CDATA[<p>Der reinterpret_cast ist unnoetig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2243324</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2243324</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Sun, 19 Aug 2012 14:42:12 GMT</pubDate></item></channel></rss>