<?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[const std::vector?]]></title><description><![CDATA[<p>Hi,</p>
<p>was wäre wohl die beste Möglichkeit ein konstantes Array von Objekten ohne Default-Konstruktor zu erstellen? C++0x mit</p>
<pre><code class="language-cpp">const std::vector&lt;Klasse&gt; v = ({2, 3, 5, 7, 11}); // http://stackoverflow.com/questions/3701903/initialisation-of-static-vector
</code></pre>
<p>steht nicht zur Verfügung. Boost für list_of() auch nicht. Wie gesagt, der Container ist mir egal, es könnte auch ein rohes Array sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/290543/const-std-vector</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 10:06:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/290543.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 28 Jul 2011 16:16:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 16:16:30 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>was wäre wohl die beste Möglichkeit ein konstantes Array von Objekten ohne Default-Konstruktor zu erstellen? C++0x mit</p>
<pre><code class="language-cpp">const std::vector&lt;Klasse&gt; v = ({2, 3, 5, 7, 11}); // http://stackoverflow.com/questions/3701903/initialisation-of-static-vector
</code></pre>
<p>steht nicht zur Verfügung. Boost für list_of() auch nicht. Wie gesagt, der Container ist mir egal, es könnte auch ein rohes Array sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2099087</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099087</guid><dc:creator><![CDATA[threadersteller]]></dc:creator><pubDate>Thu, 28 Jul 2011 16:16:30 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 16:21:39 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">my_class const array[] = { my_class(0), my_class(1), my_class(2) };

// Falls gewünscht dann
template&lt;typename T, std::size_t N&gt; std::size_t array_size(T(&amp;)[N]) { return N; }
std::vector&lt;my_class&gt; const vec(array, array + array_size(array));
</code></pre>
<p>Einen Vektor direkt mit solchem Inhalt zu initialisieren ist in C++03 meines Wissens nicht möglich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2099088</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099088</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Thu, 28 Jul 2011 16:21:39 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 16:29:14 GMT]]></title><description><![CDATA[<p>Warum steht boost::list_of nicht zur Verfügung? Ist ja Header-only das kannst du einfach kopieren. Ansonsten kann man eine billige Variante davon ja auch selbst schreiben:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
struct vector_of_helper {
  std::vector&lt;T&gt; v;
  vector_of_helper &amp;operator()(T const &amp;t) {
    v.push_back(t);
    return *this;
  }
  operator std::vector&lt;T&gt;() { return v; }
  vector_of_helper(T const &amp;t) {
    v.push_back(t);
  }
};

template&lt;typename T&gt;
vector_of_helper&lt;T&gt; vector_of(T const &amp;t) {
  return vector_of_helper&lt;T&gt;(t);
}

// ...
std::vector&lt;int&gt; const v = vector_of(1)(2)(3);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2099089</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099089</guid><dc:creator><![CDATA[rüdiger]]></dc:creator><pubDate>Thu, 28 Jul 2011 16:29:14 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 16:35:57 GMT]]></title><description><![CDATA[<p>Äh, danke für die schnelle Antwort, aber ich habe irgendwie vergessen den Hauptpunkt meiner Frage zu stellen. oO<br />
Die erste Version oben wäre natürlich perfekt, nur soll das ein nicht-statischer Member einer Klasse werden und Folgendes geht leider nicht:</p>
<pre><code class="language-cpp">struct test
{
  test()
    : k({Klasse(5), Klasse(4)})
  {}
  const Klasse k[];
};
</code></pre>
<p>Sieht jemand einen besseren Ausweg als:</p>
<pre><code class="language-cpp">struct test
{
  test()
  {
    k.push_back(5);
    ..
  }
  std::vector&lt;Klasse&gt; k;
};
</code></pre>
<p>Perfekt wäre natürlich so was wie:</p>
<pre><code class="language-cpp">struct test
{
  const Klasse k[] = {Klasse(5), Klasse(4)};
};
</code></pre>
<p>Warum haben die Standardler das eigentlich verboten? Es ist doch eindeutig, was der Konstruktor hier machen muss.</p>
<p>Ich sehe gerade: vector_of könnte eine Lösung sein..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2099093</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099093</guid><dc:creator><![CDATA[threadersteller]]></dc:creator><pubDate>Thu, 28 Jul 2011 16:35:57 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 17:02:09 GMT]]></title><description><![CDATA[<p>Wenn der Arrayinhalt konstant ist, gibt es dann einen bestimmten Grund, dass er nicht statisch sein kann? Das sieht man eigentlich eher selten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2099101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099101</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Thu, 28 Jul 2011 17:02:09 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 17:15:38 GMT]]></title><description><![CDATA[<p>seldon schrieb:</p>
<blockquote>
<p>Wenn der Arrayinhalt konstant ist, gibt es dann einen bestimmten Grund, dass er nicht statisch sein kann? Das sieht man eigentlich eher selten.</p>
</blockquote>
<p>Hm.. vielleicht bin ich da auch gerade von etwas Falschem ausgegangen. Ist das Thread-safe? Ich weiß, der heilige Standard sagt da nichts zu, aber wie sieht es in der Praxis aus?</p>
<pre><code class="language-cpp">void thread()
{
  my_class c;
  c.do_smth_cool();
}

int main()
{
  start_thread(thread);
  my_class c;
  c.do_smth_cool();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2099106</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099106</guid><dc:creator><![CDATA[threadersteller]]></dc:creator><pubDate>Thu, 28 Jul 2011 17:15:38 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 17:19:12 GMT]]></title><description><![CDATA[<p>Probleme mit Threads kannst du nur bekommen, wenn mindestens einer der Beteiligten schreiben will - bei reinen Lesezugriffen hast du dort keine Probleme.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2099107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099107</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 28 Jul 2011 17:19:12 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 17:42:27 GMT]]></title><description><![CDATA[<p>threadersteller schrieb:</p>
<blockquote>
<p>seldon schrieb:</p>
<blockquote>
<p>Wenn der Arrayinhalt konstant ist, gibt es dann einen bestimmten Grund, dass er nicht statisch sein kann? Das sieht man eigentlich eher selten.</p>
</blockquote>
<p>Hm.. vielleicht bin ich da auch gerade von etwas Falschem ausgegangen. Ist das Thread-safe? Ich weiß, der heilige Standard sagt da nichts zu, aber wie sieht es in der Praxis aus?</p>
<pre><code class="language-cpp">void thread()
{
  my_class c;
  c.do_smth_cool();
}

int main()
{
  start_thread(thread);
  my_class c;
  c.do_smth_cool();
}
</code></pre>
</blockquote>
<p>Wenn <code>my_class</code> keine statischen Member hat, ist das typischerweise threadsafe. Sind ja zwei unterschiedliche Instanzen.<br />
Natürlich kannst du Probleme bekommen wenn <code>do_smth_cool()</code> (direkt oder indirekt) auf andere gemeinsam genutzte Daten zugreift, z.B. globale Variablen oder statische Member anderer Klassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2099120</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099120</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Thu, 28 Jul 2011 17:42:27 GMT</pubDate></item><item><title><![CDATA[Reply to const std::vector? on Thu, 28 Jul 2011 20:15:09 GMT]]></title><description><![CDATA[<p>Wie wäre es mit einer kleinen Streamklasse, die automatisch in einen std::vector umwandelbar ist?<br />
Man könnte natürlich (gibt's bei boost afaik auch) auch den Kommaoperator überladen (eine Taste weniger <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>
<pre><code class="language-cpp">#include &quot;VectorStream.hpp&quot;

class MyClass
{
    int value;
    public:
    MyClass(int);
};

class Foo
{
    std::vector&lt;MyClass&gt; bar;
    public:
    Foo() : bar(VectorStream&lt;MyClass&gt;() &lt;&lt; 2 &lt;&lt; 5 &lt;&lt; 8 &lt;&lt; 11 &lt;&lt; 14 &lt;&lt; 17 &lt;&lt; 20) {}
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2099194</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2099194</guid><dc:creator><![CDATA[wxSkip]]></dc:creator><pubDate>Thu, 28 Jul 2011 20:15:09 GMT</pubDate></item></channel></rss>