<?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[boost::variant - immer erreichbare Instanzen der Template-argumente]]></title><description><![CDATA[<p>Hohoho! :xmas2:</p>
<p>Hab endlich Boost 1.48 gebaut und mich gleich auf <a href="http://www.highscore.de/cpp/boost/datenstrukturen.html#datenstrukturen_any" rel="nofollow">dieses</a> Tutorial begeben.</p>
<p>Da hab ich boost::variant gefunden. Dieser Code compiliert und <strong>funktioniert</strong> einwandfrei:</p>
<pre><code class="language-cpp">#include &lt;boost/variant.hpp&gt;
#include &lt;iostream&gt;

int main()
{
  boost::variant&lt;std::string, int&gt; a;
  a = std::string(&quot;hellao&quot;);
  std::cout &lt;&lt; boost::get&lt;std::string&gt;(a) &lt;&lt; '\n';
  a = 5;
  std::cout &lt;&lt; boost::get&lt;int&gt;(a) &lt;&lt; '\n';
}
</code></pre>
<p>Dieser (so leid es mir tut) nicht:</p>
<pre><code class="language-cpp">#include &lt;boost/variant.hpp&gt;
#include &lt;iostream&gt;

int main()
{
  boost::variant&lt;std::string, int&gt; a;
  a = std::string(&quot;hellao&quot;);
  a = 5;
  std::cout &lt;&lt; boost::get&lt;std::string&gt;(a) &lt;&lt; '\n';
  std::cout &lt;&lt; boost::get&lt;int&gt;(a) &lt;&lt; '\n';
}
</code></pre>
<p>Versteht jemand, was ich meine? Ich will, dass die Klasse für jeden Datentyp (den ich als Template-Parameter übergeben hab) eine immer erreichbare Instanz anlegt. Das tut er anscheinend nicht, was aber jetzt genau? Könnt's mir vielleicht jemand erklären?</p>
<p>THX (&amp; im V*r**s)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/297854/boost-variant-immer-erreichbare-instanzen-der-template-argumente</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 06:49:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/297854.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 08 Jan 2012 14:05:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to boost::variant - immer erreichbare Instanzen der Template-argumente on Sun, 08 Jan 2012 14:05:24 GMT]]></title><description><![CDATA[<p>Hohoho! :xmas2:</p>
<p>Hab endlich Boost 1.48 gebaut und mich gleich auf <a href="http://www.highscore.de/cpp/boost/datenstrukturen.html#datenstrukturen_any" rel="nofollow">dieses</a> Tutorial begeben.</p>
<p>Da hab ich boost::variant gefunden. Dieser Code compiliert und <strong>funktioniert</strong> einwandfrei:</p>
<pre><code class="language-cpp">#include &lt;boost/variant.hpp&gt;
#include &lt;iostream&gt;

int main()
{
  boost::variant&lt;std::string, int&gt; a;
  a = std::string(&quot;hellao&quot;);
  std::cout &lt;&lt; boost::get&lt;std::string&gt;(a) &lt;&lt; '\n';
  a = 5;
  std::cout &lt;&lt; boost::get&lt;int&gt;(a) &lt;&lt; '\n';
}
</code></pre>
<p>Dieser (so leid es mir tut) nicht:</p>
<pre><code class="language-cpp">#include &lt;boost/variant.hpp&gt;
#include &lt;iostream&gt;

int main()
{
  boost::variant&lt;std::string, int&gt; a;
  a = std::string(&quot;hellao&quot;);
  a = 5;
  std::cout &lt;&lt; boost::get&lt;std::string&gt;(a) &lt;&lt; '\n';
  std::cout &lt;&lt; boost::get&lt;int&gt;(a) &lt;&lt; '\n';
}
</code></pre>
<p>Versteht jemand, was ich meine? Ich will, dass die Klasse für jeden Datentyp (den ich als Template-Parameter übergeben hab) eine immer erreichbare Instanz anlegt. Das tut er anscheinend nicht, was aber jetzt genau? Könnt's mir vielleicht jemand erklären?</p>
<p>THX (&amp; im V*r**s)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2165310</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2165310</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 08 Jan 2012 14:05:24 GMT</pubDate></item><item><title><![CDATA[Reply to boost::variant - immer erreichbare Instanzen der Template-argumente on Sun, 08 Jan 2012 14:11:27 GMT]]></title><description><![CDATA[<p>Eine variant ist im Prinzip eine union ohne deren Probleme. Du suchst nicht etwa std::tuple?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2165312</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2165312</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sun, 08 Jan 2012 14:11:27 GMT</pubDate></item><item><title><![CDATA[Reply to boost::variant - immer erreichbare Instanzen der Template-argumente on Sun, 08 Jan 2012 14:22:20 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Eine variant ist im Prinzip eine union ohne deren Probleme. Du suchst nicht etwa std::tuple?</p>
</blockquote>
<p>Seh ich mir gleich an, thx</p>
<p>Edit: jup, das war's</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2165329</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2165329</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Sun, 08 Jan 2012 14:22:20 GMT</pubDate></item><item><title><![CDATA[Reply to boost::variant - immer erreichbare Instanzen der Template-argumente on Mon, 09 Jan 2012 17:53:30 GMT]]></title><description><![CDATA[<p>Ich hoffe, ich habe Aufgabe 1 richtig gelöst:</p>
<blockquote>
<p>Definieren Sie einen Datentyp configuration, der Name-Wert-Paare speichern kann. Namen sind dabei vom Typ std::string, Werte vom Typ std::string, int oder float. Speichern Sie dann in der Funktion main() folgende Name-Wert-Paare in einem Objekt vom Typ configuration: path=C:\Windows, version=3 und pi=3.1415. Geben Sie dann die Name-Wert-Paare zum Test auf die Standardausgabe aus.</p>
</blockquote>
<pre><code class="language-cpp">#include &lt;boost/variant.hpp&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;utility&gt;

using std::string;
using std::ostream;

class configuration
{
    std::pair&lt;string, boost::variant&lt;string, int, float&gt;&gt; values;

public:

    configuration() = delete;
    configuration(const string&amp; key, int value)             : values(std::make_pair(key, value)) {}
    configuration(const string&amp; key, float value)           : values(std::make_pair(key, value)) {}
    configuration(const string&amp; key, const string&amp; value)   : values(std::make_pair(key, value)) {}

    friend ostream&amp; operator&lt;&lt;(ostream&amp;, const configuration&amp;);
};

ostream&amp; operator&lt;&lt;(ostream&amp; os, const configuration&amp; c)
{
    return os &lt;&lt; c.values.first &lt;&lt; &quot; = &quot; &lt;&lt; c.values.second;
}

int main()
{
    std::cout &lt;&lt; configuration (&quot;path&quot;, &quot;C:\\Windows&quot;) &lt;&lt; '\n'
              &lt;&lt; configuration (&quot;pi&quot;, 3.1415f)         &lt;&lt; '\n'
              &lt;&lt; configuration (&quot;version&quot;, 3)          &lt;&lt; '\n';
}
</code></pre>
<p>Edit: und Aufgabe 2:</p>
<blockquote>
<p>Erweitern Sie Ihre Lösung zu Aufgabe 1 dahingehend, dass nach der Ausgabe der Name-Wert-Paare path auf den neuen Wert C:\Windows\System gesetzt wird. Geben Sie anschließend zum Test den neuen Wert von path auf die Standardausgabe aus.</p>
</blockquote>
<pre><code class="language-cpp">#include &lt;boost/variant.hpp&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;utility&gt;

using std::string;
using std::ostream;

class configuration
{
    std::pair&lt;string, boost::variant&lt;string, int, float&gt;&gt; values;

public:

    configuration() = delete;
    configuration(const string&amp; key, int value)             : values(std::make_pair(key, value)) {}
    configuration(const string&amp; key, float value)           : values(std::make_pair(key, value)) {}
    configuration(const string&amp; key, const string&amp; value)   : values(std::make_pair(key, value)) {}

    friend ostream&amp; operator&lt;&lt;(ostream&amp;, const configuration&amp;);

    void set_value(const boost::variant&lt;string, int, float&gt;&amp; var)
    {
        values.second = var;
    }

    void get_value(const boost::variant&lt;string, int, float&gt;&amp; var) const
    {
        return boost::get&lt;
    }
};

ostream&amp; operator&lt;&lt;(ostream&amp; os, const configuration&amp; c)
{
    return os &lt;&lt; c.values.first &lt;&lt; &quot; = &quot; &lt;&lt; c.values.second;
}

int main()
{
    configuration path(&quot;path&quot;, &quot;C:\\Windows&quot;);
    std::cout &lt;&lt; path &lt;&lt; '\n';
    path.set_value(&quot;C:\\Windows\\system&quot;);
    std::cout &lt;&lt; path &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2165383</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2165383</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 09 Jan 2012 17:53:30 GMT</pubDate></item><item><title><![CDATA[Reply to boost::variant - immer erreichbare Instanzen der Template-argumente on Sun, 08 Jan 2012 16:38:40 GMT]]></title><description><![CDATA[<blockquote>
<p>Definieren Sie einen Datentyp configuration, der Name-Wert-Paare speichern kann. Namen sind dabei vom Typ std::string, Werte vom Typ std::string, int oder float. Speichern Sie dann in der Funktion main() folgende Name-Wert-Paare in einem Objekt vom Typ configuration: path=C:\Windows, version=3 und pi=3.1415. Geben Sie dann die Name-Wert-Paare zum Test auf die Standardausgabe aus.</p>
</blockquote>
<pre><code class="language-cpp">template&lt;typename T&gt;
struct configuration
{
  T value;
  std::string name;

  configuration(const std::string&amp; name, const T&amp; value)
  : name(name), value(value)
  {}
};

#include &lt;iostream&gt;

std::ostream&amp; operator&lt;&lt; (std::ostream&amp; s, const configuration&amp; config)
{
  s &lt;&lt; config.name &lt;&lt; &quot; = &quot; &lt;&lt; config.value;
  return s;
}

#include &lt;string&gt;

int main()
{
  configuration&lt;float&gt; pi(&quot;pi&quot;, 3.1415);
  configuration&lt;int&gt; version(&quot;version&quot;, 3);
  configuration&lt;std::string&gt; path(&quot;path&quot;, &quot;C:/windows&quot;);

  std::cout &lt;&lt; pi &lt;&lt; '\n' &lt;&lt; version &lt;&lt; '\n' &lt;&lt; path &lt;&lt; std::endl;
}
</code></pre>
<blockquote>
<p>Erweitern Sie Ihre Lösung zu Aufgabe 1 dahingehend, dass nach der Ausgabe der Name-Wert-Paare path auf den neuen Wert C:\Windows\System gesetzt wird. Geben Sie anschließend zum Test den neuen Wert von path auf die Standardausgabe aus.</p>
</blockquote>
<pre><code class="language-cpp">int main()
{
  configuration&lt;float&gt; pi(&quot;pi&quot;, 3.1415);
  configuration&lt;int&gt; version(&quot;version&quot;, 3);
  configuration&lt;std::string&gt; path(&quot;path&quot;, &quot;C:/windows&quot;);

  std::cout &lt;&lt; pi &lt;&lt; '\n' &lt;&lt; version &lt;&lt; '\n' &lt;&lt; path &lt;&lt; std::endl;

  path.value = &quot;C:/Windows/System&quot;;
  std::cout &lt;&lt; path &lt;&lt; std::endl;
}
</code></pre>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2165395</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2165395</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 08 Jan 2012 16:38:40 GMT</pubDate></item><item><title><![CDATA[Reply to boost::variant - immer erreichbare Instanzen der Template-argumente on Sun, 08 Jan 2012 21:40:26 GMT]]></title><description><![CDATA[<blockquote>
<p>Definieren Sie einen Datentyp configuration, der Name-Wert-<strong>Paare</strong> speichern kann.</p>
</blockquote>
<p>Das klingt für mich so, als solle der Detentyp mehrere Paare abspeichern können. Macht für eine Konfiguration ja auch mehr Sinn.</p>
<pre><code class="language-cpp">typedef std::map&lt;std::string, boost::variant&lt;std::string, int, float&gt;&gt; configuration;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2165579</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2165579</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Sun, 08 Jan 2012 21:40:26 GMT</pubDate></item><item><title><![CDATA[Reply to boost::variant - immer erreichbare Instanzen der Template-argumente on Mon, 09 Jan 2012 06:18:17 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>Keine Datenkapselung, unnötige Templates. Hier wirst du noch was werfen müssen wenn feststeht dass auch wirklich nur floats, int und strings als Paarwert verwendet werden dürfen... :xmas1:</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2165634</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2165634</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 09 Jan 2012 06:18:17 GMT</pubDate></item></channel></rss>