<?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[Problem mit eigener Vectorklasse]]></title><description><![CDATA[<p>Das folgende minimale Beispiel:</p>
<pre><code>#include &lt;algorithm&gt;

template&lt;class T&gt;
class vector
{
	T* ptr;
	unsigned sz;
public:
	vector(unsigned n, const T&amp; fill)
	: ptr(new T[n]), sz(n) {
		std::fill(ptr, ptr + sz, fill);
	}
};

struct foo
{
	foo(int) { }
};

int main()
{
	vector&lt;foo&gt; bar(5, 2);
}
</code></pre>
<p>Wie kann ich den Konstruktor mit n und mit fill fixen, sodass er so funktioniert wie erwartet und den Nicht-Standard-Konstruktor von foo aufruft?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/326365/problem-mit-eigener-vectorklasse</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 12:45:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326365.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 14 Jun 2014 13:34:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Sat, 14 Jun 2014 13:34:47 GMT]]></title><description><![CDATA[<p>Das folgende minimale Beispiel:</p>
<pre><code>#include &lt;algorithm&gt;

template&lt;class T&gt;
class vector
{
	T* ptr;
	unsigned sz;
public:
	vector(unsigned n, const T&amp; fill)
	: ptr(new T[n]), sz(n) {
		std::fill(ptr, ptr + sz, fill);
	}
};

struct foo
{
	foo(int) { }
};

int main()
{
	vector&lt;foo&gt; bar(5, 2);
}
</code></pre>
<p>Wie kann ich den Konstruktor mit n und mit fill fixen, sodass er so funktioniert wie erwartet und den Nicht-Standard-Konstruktor von foo aufruft?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2403871</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403871</guid><dc:creator><![CDATA[MrFrager]]></dc:creator><pubDate>Sat, 14 Jun 2014 13:34:47 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Sat, 14 Jun 2014 13:42:55 GMT]]></title><description><![CDATA[<blockquote>
<p>Wie kann ich den Konstruktor mit n und mit fill fixen, sodass er so funktioniert wie erwartet und den Nicht-Standard-Konstruktor von foo aufruft?</p>
</blockquote>
<p>Indem du zuerst nur den Speicher allokierst und dann nacheinander mit placement-new durchgehst und jedes Element einzeln konstruierst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2403872</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403872</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 14 Jun 2014 13:42:55 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Sat, 14 Jun 2014 18:47:03 GMT]]></title><description><![CDATA[<p>Grobes Beispiel:</p>
<pre><code>#include &lt;algorithm&gt;

template&lt;class T&gt;
class vector
{
	T* ptr;
	std::size_t sz; /// Sollte wohl eher size_t sein
public:

	static_assert( std::is_nothrow_destructible&lt;T&gt;::value, &quot;&quot; );

	/// Definiert automatisch Kopierkonstruktor und -zuweisungsoperator als deleted.
	vector( vector&amp;&amp; ) = default;
	vector&amp; operator=( vector&amp;&amp; ) = default;

	vector(std::size_t n, const T&amp; fill) :
		ptr( static_cast&lt;T*&gt;(operator new(n * sizeof(T))) ),
		sz(n)
	{
		for( std::size_t i = 0; i != sz; ++i ) try
		{
			new (ptr + i) T(fill);
		}
		catch( ... )
		{
			for( auto ic = i; ic--; )
				(ptr + ic)-&gt;~T();
			operator delete(ptr);
			throw;
		}
	}

	~vector()
	{
		for( std::size_t i = 0; i != sz; ++i )
			ptr[i].~T();
		operator delete(ptr);
	}
};

#include &lt;iostream&gt;

struct foo
{
	foo(int) { std::cout &lt;&lt; &quot;Ctor!\n&quot;; }
	foo(foo const&amp;) { std::cout &lt;&lt; &quot;Copy-Ctor!\n&quot;; }
	~foo() { std::cout &lt;&lt; &quot;Dtor!\n&quot;; }
};

int main()
{
	vector&lt;foo&gt; bar(5, 2);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2403875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403875</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 14 Jun 2014 18:47:03 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Sat, 14 Jun 2014 18:50:22 GMT]]></title><description><![CDATA[<pre><code>throw; // Passiert auch implizit
</code></pre>
<p>nur wenn du noch ein {}-Paar entfernst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2403917</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403917</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 14 Jun 2014 18:50:22 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Sat, 14 Jun 2014 18:46:35 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<pre><code>throw; // Passiert auch implizit
</code></pre>
<p>nur wenn du noch ein {}-Paar entfernst.</p>
</blockquote>
<p>Ja, fällt mir auch gerade auf. Korrigiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2403918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2403918</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Sat, 14 Jun 2014 18:46:35 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Mon, 16 Jun 2014 11:42:32 GMT]]></title><description><![CDATA[<p>Vielen Dank, Arcoth, der konkrete Code hat mir sehr geholfen, auch weil ich es vermutlich vergessen hätte die Destruktoren aufzurufen wenn ein Konstruktor wirft.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404131</guid><dc:creator><![CDATA[MrFrager]]></dc:creator><pubDate>Mon, 16 Jun 2014 11:42:32 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Mon, 16 Jun 2014 12:00:56 GMT]]></title><description><![CDATA[<p>Siehe: <code>uninitialized_fill()</code> , bzw. <code>uninitialized_fill_n()</code> in &lt;memory&gt;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404134</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404134</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Mon, 16 Jun 2014 12:00:56 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Mon, 16 Jun 2014 22:40:31 GMT]]></title><description><![CDATA[<p>MrFrager schrieb:</p>
<blockquote>
<p>Vielen Dank, Arcoth, der konkrete Code hat mir sehr geholfen, auch weil ich es vermutlich vergessen hätte die Destruktoren aufzurufen wenn ein Konstruktor wirft.</p>
</blockquote>
<p>Den Code aber nicht 1:1 kopieren. Das war lediglich zu Demonstrationszwecken ausgeschrieben - um zu verdeutlichen wie das ganze (intern) aussehen muss.</p>
<p>Im richtigen Code nimmst du natürlich<a href="http://en.cppreference.com/w/cpp/memory/uninitialized_fill_n" rel="nofollow"> <code>std::uninitialized_fill_n</code> </a>, wie von Furble vorgeschlagen, denn das tut exakt das was ich in den Konstruktor geschrieben habe (abgesehen von der Speicherfreigabe, wie unten von camper angemerkt... :D):</p>
<pre><code>vector(std::size_t n, const T&amp; fill) :
        ptr( static_cast&lt;T*&gt;(operator new(n * sizeof(T))) ),
        sz(n)
    {
        try
        {
            std::uninitialized_fill_n( ptr, sz, fill );
        }
        catch( ... )
        {
            delete ptr;
            throw;
        }
    }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2404185</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404185</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Mon, 16 Jun 2014 22:40:31 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit eigener Vectorklasse on Mon, 16 Jun 2014 16:04:34 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Im richtigen Code nimmst du natürlich<a href="http://en.cppreference.com/w/cpp/memory/uninitialized_fill_n" rel="nofollow"> <code>std::uninitialized_fill_n</code> </a>, wie von Furble vorgeschlagen, denn das tut exakt das was ich in den Konstruktor geschrieben habe:</p>
<pre><code>vector(std::size_t n, const T&amp; fill) :
        ptr( static_cast&lt;T*&gt;(operator new(n * sizeof(T))) ),
        sz(n)
    {
        std::uninitialized_fill_n( ptr, sz, fill );
    }
</code></pre>
</blockquote>
<p>Speicherfreigabe vergessen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404187</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404187</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 16 Jun 2014 16:04:34 GMT</pubDate></item></channel></rss>