<?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 const-correctness und std::map::find]]></title><description><![CDATA[<p>Hallo!</p>
<p>Habe ein Problem mit der const-correctness zweier meiner Klassen, ich vermute, dass es von der Art und Weise herrührt, wie ich std:<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f5fa.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--world_map"
      title=":map:"
      alt="🗺"
    />:find benutze. Dort gibt es jedenfalls immer einen Fehler beim Kompilieren, den ich mir jedoch nicht erklären kann.</p>
<p>Hier mal ein (hoffentlich) aussagekräftiger Codeschnipsel:</p>
<pre><code class="language-cpp">/// Count how many pairs are waiting for processing.
ULong_t Sni3AnalysisStorage::getNoUnprocessedPairs(const SN_Analysis_t* pcaller) const
{
	if ( mColumnBufferDeque.empty() )
	{
		return 0;
	}
	/**
	 * I use a map with pointers as key values and iterators as mapped values.
	 */
	std::map&lt; SN_Analysis_t*, std::deque&lt;Sni3ColumnDataPair&gt;::iterator &gt;::const_iterator it;
	/**
	 * Below, on the next line I always get a cryptic compiler error, but only as long as &quot;pcaller&quot; is const:
	 */
	it = mBufferAnalysisMap.find( pcaller );
        /// this is strange, because std::map::find has two signatures:
        /// iterator find ( const key_type&amp; x );
        /// const_iterator find ( const key_type&amp; x ) const;
	if ( it != mBufferAnalysisMap.end() )
	{
		std::deque&lt;Sni3ColumnDataPair&gt;::const_iterator dit = it-&gt;second;
		return std::distance( dit, mColumnBufferDeque.end() );
	}
	else
	{
		return 0;
	}
}

/// Sni3SicoAnalysis is derived from SN_Analysis_t. And because ceckBufferLength() is const, the this-pointer inside is const, too.
/// Therefore I need the argument of getNoUnprocessedPairs to be const...
void Sni3SicoAnalysis::checkBufferLength()const
{
	ULong_t noPairs = mpAnalysisStorage-&gt;getNoUnprocessedPairs(this);
	if ( noPairs == mSicoDelta )
	{
	 /// Everything is fine.
	}
	else
	{
	/// Find out, what went wrong and throw an exception!
	///....
	}
}
</code></pre>
<p>Kann mir jemand einen Rat geben, wie ich an dieser Stelle weiterkommen könnte, ohne auf const-correctness zu verzichten und ohne die pointer in meiner map auch const zu setzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/242507/problem-mit-const-correctness-und-std-map-find</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 00:51:21 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/242507.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 04 Jun 2009 15:28:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit const-correctness und std::map::find on Thu, 04 Jun 2009 15:28:25 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Habe ein Problem mit der const-correctness zweier meiner Klassen, ich vermute, dass es von der Art und Weise herrührt, wie ich std:<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f5fa.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--world_map"
      title=":map:"
      alt="🗺"
    />:find benutze. Dort gibt es jedenfalls immer einen Fehler beim Kompilieren, den ich mir jedoch nicht erklären kann.</p>
<p>Hier mal ein (hoffentlich) aussagekräftiger Codeschnipsel:</p>
<pre><code class="language-cpp">/// Count how many pairs are waiting for processing.
ULong_t Sni3AnalysisStorage::getNoUnprocessedPairs(const SN_Analysis_t* pcaller) const
{
	if ( mColumnBufferDeque.empty() )
	{
		return 0;
	}
	/**
	 * I use a map with pointers as key values and iterators as mapped values.
	 */
	std::map&lt; SN_Analysis_t*, std::deque&lt;Sni3ColumnDataPair&gt;::iterator &gt;::const_iterator it;
	/**
	 * Below, on the next line I always get a cryptic compiler error, but only as long as &quot;pcaller&quot; is const:
	 */
	it = mBufferAnalysisMap.find( pcaller );
        /// this is strange, because std::map::find has two signatures:
        /// iterator find ( const key_type&amp; x );
        /// const_iterator find ( const key_type&amp; x ) const;
	if ( it != mBufferAnalysisMap.end() )
	{
		std::deque&lt;Sni3ColumnDataPair&gt;::const_iterator dit = it-&gt;second;
		return std::distance( dit, mColumnBufferDeque.end() );
	}
	else
	{
		return 0;
	}
}

/// Sni3SicoAnalysis is derived from SN_Analysis_t. And because ceckBufferLength() is const, the this-pointer inside is const, too.
/// Therefore I need the argument of getNoUnprocessedPairs to be const...
void Sni3SicoAnalysis::checkBufferLength()const
{
	ULong_t noPairs = mpAnalysisStorage-&gt;getNoUnprocessedPairs(this);
	if ( noPairs == mSicoDelta )
	{
	 /// Everything is fine.
	}
	else
	{
	/// Find out, what went wrong and throw an exception!
	///....
	}
}
</code></pre>
<p>Kann mir jemand einen Rat geben, wie ich an dieser Stelle weiterkommen könnte, ohne auf const-correctness zu verzichten und ohne die pointer in meiner map auch const zu setzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721172</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721172</guid><dc:creator><![CDATA[goesta]]></dc:creator><pubDate>Thu, 04 Jun 2009 15:28:25 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit const-correctness und std::map::find on Thu, 04 Jun 2009 15:43:53 GMT]]></title><description><![CDATA[<p>1. Wieso gebt ihr uns nie die Fehlermeldungen? Für uns sind die nicht kryptisch, wir können die lesen und sie helfen uns.</p>
<ol start="2">
<li></li>
</ol>
<p>Statt <code>SN_Analysis_t*</code> als Key in der Map, nimmst du <code>SN_Analysis_t const*</code> . Eine andere &quot;sinnvolle&quot; Lösung gibt es nicht. Denn:</p>
<pre><code class="language-cpp">int a;
int const* cp = &amp;a;
int* p = cp; // Fehler! Wieso, sollte klar sein.
</code></pre>
<p>3. Das hat auch nichts mit den Funktionssignaturen von <code>find</code> zu tun.</p>
<p>4. const-correctness heisst übrigens nicht einfach überall <code>const</code> zu verwenden, sondern dort wo es nötig ist <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>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721179</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721179</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 04 Jun 2009 15:43:53 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit const-correctness und std::map::find on Thu, 04 Jun 2009 16:42:51 GMT]]></title><description><![CDATA[<p>Dravere schrieb:</p>
<blockquote>
<p>1. Wieso gebt ihr uns nie die Fehlermeldungen? Für uns sind die nicht kryptisch, wir können die lesen und sie helfen uns.</p>
</blockquote>
<p><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="😃"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
<p>Const-Correctness wird oft für get-Methoden benutzt, bzw. für Methoden, in denen nichts verändert wird.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

class Test
{
    private:
        int testvariable;
    public:
        Test() : testvariable(5) {}
        int testmethode() const
        {
            return testvariable;
        }
};

int main()
{
    Test t;
    t.testmethode();
}
</code></pre>
<p>Versuchst du jetzt sowas hier</p>
<pre><code class="language-cpp">int testmethode() const
{
    testvariable = 5;
    return testvariable;
}
</code></pre>
<p>Solltest du eine Fehlermeldung dieser Art bekommen:</p>
<blockquote>
<p>error: assignment of data-member `Test::testvariable' in read-only structure</p>
</blockquote>
<p>Und da steht es &quot;read-only&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721230</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721230</guid><dc:creator><![CDATA[freaky]]></dc:creator><pubDate>Thu, 04 Jun 2009 16:42:51 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit const-correctness und std::map::find on Thu, 04 Jun 2009 17:12:07 GMT]]></title><description><![CDATA[<p>Wo man const-Methoden verwendet ist mir durchaus klar gewesen, ich habe ja auch meine eigene Methode genau deshalb const gemacht, weil sie nur überprüfen muss, ob ein key in der map enthalten ist, was ja noch keine Werte ändern dürfte... Mein Punkt ist vielmehr der folgende:</p>
<pre><code class="language-cpp">class A
{
	public:	bool hasKey(const int* key)const; // guter Kandidat für const-Methode...
	private: std::map&lt;int*,string&gt; mMap;
};

// Das klappt nicht...
bool A::hasKey(const int* key)const
{
	std::map&lt;int*,string&gt;::const_iterator iter;
	iter = mMap.find(key); // ERROR (das hier ist Zeile 32)
	if ( iter == mMap.end() ) return true;
	else return false;
}

class B
{
	public:	bool hasKey(const int key)const;
	private: std::map&lt;int,string&gt; mMap;
};

// Hier klappt es tadellos...
bool B::hasKey(const int key)const
{
	std::map&lt;int,string&gt;::const_iterator iter;
	iter = mMap.find(key); // OK
	if ( iter == mMap.end() ) return true;
	else return false;
}
</code></pre>
<p>Das erste Beispiel liefert:</p>
<blockquote>
<p>**** Build of configuration Linux GCC for project sni3daq ****</p>
<p>make all<br />
make all-recursive<br />
make[1]: Betrete Verzeichnis '/home/goesta/sni3daq'<br />
Making all in src<br />
make[2]: Betrete Verzeichnis '/home/goesta/sni3daq/src'<br />
Making all in common<br />
make[3]: Betrete Verzeichnis '/home/goesta/sni3daq/src/common'<br />
/bin/bash ../../libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -I../.. -I/usr/bin/root.5.20/include/root -g -O2 -pthread -m32 -I/usr/bin/root.5.20/include/root -I/usr/include/libxml2 -I/usr/include -MT Sni3AnalysisStorage.lo -MD -MP -MF .deps/Sni3AnalysisStorage.Tpo -c -o Sni3AnalysisStorage.lo Sni3AnalysisStorage.C<br />
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I../.. -I/usr/bin/root.5.20/include/root -g -O2 -pthread -m32 -I/usr/bin/root.5.20/include/root -I/usr/include/libxml2 -I/usr/include -MT Sni3AnalysisStorage.lo -MD -MP -MF .deps/Sni3AnalysisStorage.Tpo -c Sni3AnalysisStorage.C -fPIC -DPIC -o .libs/Sni3AnalysisStorage.o<br />
Sni3AnalysisStorage.C: In member function »bool A::hasKey(const int*) const«:<br />
Sni3AnalysisStorage.C:32: Fehler: Die Übergabe von »const std::map&lt;int*, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::less&lt;int*&gt;, std::allocator&lt;std::pair&lt;int* const, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;« als »this«-Argument von »typename std::_Rb_tree&lt;_Key, std::pair&lt;const _Key, _Tp&gt;, std::_Select1st&lt;std::pair&lt;const _Key, _Tp&gt; &gt;, _Compare, typename _Alloc::rebind&lt;std::pair&lt;const _Key, _Tp&gt; &gt;::other&gt;::iterator std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::find(const _Key&amp;) [with _Key = int*, _Tp = std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, _Compare = std::less&lt;int*&gt;, _Alloc = std::allocator&lt;std::pair&lt;int* const, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;]« streicht Qualifizierer<br />
Sni3AnalysisStorage.C:32: Fehler: ungültige Umwandlung von »const int*« in »int*«<br />
Sni3AnalysisStorage.C:32: Fehler: Argument 1 von »typename std::_Rb_tree&lt;_Key, std::pair&lt;const _Key, _Tp&gt;, std::_Select1st&lt;std::pair&lt;const _Key, _Tp&gt; &gt;, _Compare, typename _Alloc::rebind&lt;std::pair&lt;const _Key, _Tp&gt; &gt;::other&gt;::iterator std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::find(const _Key&amp;) [with _Key = int*, _Tp = std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, _Compare = std::less&lt;int*&gt;, _Alloc = std::allocator&lt;std::pair&lt;int* const, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;]« wird initialisiert<br />
make[3]: Verlasse Verzeichnis '/home/goesta/sni3daq/src/common'<br />
make[3]: *** [Sni3AnalysisStorage.lo] Fehler 1<br />
make[2]: *** [all-recursive] Fehler 1<br />
make[2]: Verlasse Verzeichnis '/home/goesta/sni3daq/src'<br />
make[1]: *** [all-recursive] Fehler 1<br />
make[1]: Verlasse Verzeichnis '/home/goesta/sni3daq'<br />
make: *** [all] Fehler 2</p>
</blockquote>
<p>Und das zweite Beispiel kompiliert problemlos. Das ist genau was ich nicht verstehe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721251</guid><dc:creator><![CDATA[goesta]]></dc:creator><pubDate>Thu, 04 Jun 2009 17:12:07 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit const-correctness und std::map::find on Thu, 04 Jun 2009 17:30:09 GMT]]></title><description><![CDATA[<p>Das problem ist eigentlich relativ simpel und darauf zurück zuführen das man sich das die schreibweise mit dem const anders angewöhnen sollte, weil einem der Fehler dann auch auffallen mag.</p>
<p>Deine Signatur der Method:</p>
<pre><code class="language-cpp">bool hasKey(const int* key)const;
</code></pre>
<p>bessere Schreibweise:</p>
<pre><code class="language-cpp">bool hasKey(int const * key)const;
</code></pre>
<p>'int const * key' Liest sich von Links nach Rechts:<br />
Key ist ein Zeiger auf ein konstantes Objekt vom Typ int.</p>
<p>Deine map speichert int * als Key ab (sprich Zeiger auf ein Objekt vom Typ int)</p>
<p>Key = int *<br />
und Key const &amp; entspricht dann int * const &amp;<br />
und nicht int const * const &amp;</p>
<p>Würdest du die Signatur auf:</p>
<pre><code class="language-cpp">bool hasKey(int * const key)const;
</code></pre>
<p>ändern<br />
dann funktioniert das ganze auch.</p>
<p>Ich hoffe ich hab genug Verwirrung gestiftet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1721265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1721265</guid><dc:creator><![CDATA[evilissimo]]></dc:creator><pubDate>Thu, 04 Jun 2009 17:30:09 GMT</pubDate></item></channel></rss>