<?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::) indexable]]></title><description><![CDATA[<p>Hi ^^</p>
<p>Ich habe ein Problem mit indexable (aus boost rauskopiert, weil ich es abgeben muss und keine Lust habe, 20 boost-Header mitzuliefern):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

template &lt;typename T, typename const_reference, typename size_type&gt;
struct indexable
{
	const_reference operator[](size_type n) const
	{
		return *(static_cast&lt;const T&amp;&gt;(*this) + n);
	}
};

struct IPv4 : indexable &lt;IPv4, const unsigned char&amp;, size_t&gt;
{
public:
	typedef unsigned char value_type;
private:
	typedef unsigned int saving_type;
	static const char Seperator = '.';
	value_type vals[4];
public:
	explicit IPv4(value_type a = value_type(), value_type b = value_type(), value_type c = value_type(), value_type d = value_type())
	{
		vals[0] = a;
		vals[1] = b;
		vals[2] = c;
		vals[3] = d;
	}

	value_type&amp; operator[] (size_t i)
	{
		return vals[i];
	}

	friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; s, const IPv4 &amp;these)
	{
		for (size_t i(0), e(4-1); i != e; ++i)
		{
			s &lt;&lt; static_cast &lt;IPv4::saving_type&gt; (these[i])
			  &lt;&lt; IPv4::Seperator;
		}
		s &lt;&lt; static_cast &lt;IPv4::saving_type&gt; (these[3]);
		return s;
	}
};

int main()
{
	IPv4 eins (127, 0, 0, 1);
	std::cout &lt;&lt; eins;
}
</code></pre>
<p>Der Compiler (MSVC) sagt mir jetzt aber:</p>
<p>MSVC schrieb:</p>
<blockquote>
<p>1&gt;...\test\main.cpp(38) : error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const IPv4' (or there is no acceptable conversion)<br />
1&gt; ...\test\main.cpp(29): could be 'IPv4::value_type &amp;IPv4::operator [](size_t)'<br />
1&gt; while trying to match the argument list '(const IPv4, size_t)'<br />
1&gt;...\test\main.cpp(41) : error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const IPv4' (or there is no acceptable conversion)<br />
1&gt; ...\test\main.cpp(29): could be 'IPv4::value_type &amp;IPv4::operator [](size_t)'<br />
1&gt; while trying to match the argument list '(const IPv4, int)'</p>
</blockquote>
<p>Ja - das Problem sollte eigtl offensichtlich sein und beim Code sollte es auch nichts zu kommentieren geben...</p>
<p>Hat jemand ne Idee?</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/241290/boost-indexable</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 15:16:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/241290.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 18 May 2009 17:09:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 17:09:53 GMT]]></title><description><![CDATA[<p>Hi ^^</p>
<p>Ich habe ein Problem mit indexable (aus boost rauskopiert, weil ich es abgeben muss und keine Lust habe, 20 boost-Header mitzuliefern):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

template &lt;typename T, typename const_reference, typename size_type&gt;
struct indexable
{
	const_reference operator[](size_type n) const
	{
		return *(static_cast&lt;const T&amp;&gt;(*this) + n);
	}
};

struct IPv4 : indexable &lt;IPv4, const unsigned char&amp;, size_t&gt;
{
public:
	typedef unsigned char value_type;
private:
	typedef unsigned int saving_type;
	static const char Seperator = '.';
	value_type vals[4];
public:
	explicit IPv4(value_type a = value_type(), value_type b = value_type(), value_type c = value_type(), value_type d = value_type())
	{
		vals[0] = a;
		vals[1] = b;
		vals[2] = c;
		vals[3] = d;
	}

	value_type&amp; operator[] (size_t i)
	{
		return vals[i];
	}

	friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; s, const IPv4 &amp;these)
	{
		for (size_t i(0), e(4-1); i != e; ++i)
		{
			s &lt;&lt; static_cast &lt;IPv4::saving_type&gt; (these[i])
			  &lt;&lt; IPv4::Seperator;
		}
		s &lt;&lt; static_cast &lt;IPv4::saving_type&gt; (these[3]);
		return s;
	}
};

int main()
{
	IPv4 eins (127, 0, 0, 1);
	std::cout &lt;&lt; eins;
}
</code></pre>
<p>Der Compiler (MSVC) sagt mir jetzt aber:</p>
<p>MSVC schrieb:</p>
<blockquote>
<p>1&gt;...\test\main.cpp(38) : error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const IPv4' (or there is no acceptable conversion)<br />
1&gt; ...\test\main.cpp(29): could be 'IPv4::value_type &amp;IPv4::operator [](size_t)'<br />
1&gt; while trying to match the argument list '(const IPv4, size_t)'<br />
1&gt;...\test\main.cpp(41) : error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const IPv4' (or there is no acceptable conversion)<br />
1&gt; ...\test\main.cpp(29): could be 'IPv4::value_type &amp;IPv4::operator [](size_t)'<br />
1&gt; while trying to match the argument list '(const IPv4, int)'</p>
</blockquote>
<p>Ja - das Problem sollte eigtl offensichtlich sein und beim Code sollte es auch nichts zu kommentieren geben...</p>
<p>Hat jemand ne Idee?</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1712360</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712360</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 18 May 2009 17:09:53 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 17:56:15 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">return *(static_cast&lt;const T&amp;&gt;(*this) + n);
</code></pre>
<p>sicher das das so klappt? du erhöhst ja die refernez um n und indizierst das wieder<br />
versuchs mal so</p>
<pre><code class="language-cpp">return *(reinterpret_cast&lt;const T *&gt;(this) + n);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1712388</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712388</guid><dc:creator><![CDATA[hilfehilfe]]></dc:creator><pubDate>Mon, 18 May 2009 17:56:15 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 18:12:22 GMT]]></title><description><![CDATA[<p>Was um alles in der Welt hast du da vor? Hast du mal die Dokumentation dazu gelesen? <code>boost::indexable</code> ist für Iteratoren gedacht, also <code>T</code> soll eine Iteratoren Klasse sein. Diese Iterator Klasse muss den <code>operator+</code> überladen haben.</p>
<p>Da lies selber nach:<br />
<a href="http://www.boost.org/doc/libs/1_39_0/libs/utility/operators.htm#dereference" rel="nofollow">http://www.boost.org/doc/libs/1_39_0/libs/utility/operators.htm#dereference</a></p>
<p>Oder vielleicht wäre dieser Artikel noch interessant:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-240706.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-240706.html</a></p>
<p>Jedenfalls verwendest du <code>boost::indexable</code> definitiv falsch.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1712401</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712401</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 18 May 2009 18:12:22 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 18:20:14 GMT]]></title><description><![CDATA[<p>hilfehilfe schrieb:</p>
<blockquote>
<p>sicher das das so klappt? du erhöhst ja die refernez um n und indizierst das wieder</p>
</blockquote>
<p>Die Umsetzung ist korrekt, die Verwendung nicht.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/16305">@unskilled</a>:<br />
um indexable&lt;T, R, D&gt; zu verwenden, muss <code>operator+ (T, D)</code> und <code>T::operator*()</code> definiert sein, und zwar so dass die Addition konvertierbar in T ist und die Dereferenzierung etwas konvertierbares in R liefert. Schau dir am Besten die Beispiele in der Doku nochmal an.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1712408</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712408</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 18 May 2009 18:20:14 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 18:39:26 GMT]]></title><description><![CDATA[<p>verdammt - hätt ich auch selbst drauf kommen können xD<br />
ty ^^</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/9752">@dravere</a>: den artikel hab ich scho gelesen - allerdings ist indexable da nicht gezeigt worden - und den einen satz, wo es tatsächlich steht, hab ich iwie net gelesen -.-</p>
<p>allerdings hab ich in der boost-doku kein bsp gefunden - vll bin ich ja auch einfach nur blind, aber eigtl hab ich doch nen ganzes weilchen gesucht ^^</p>
<p>bb</p>
<p>edit: eigtl such ich was, was mir die arbeit abnimmt, op[] immer einmal für const und einmal für non-const zu überladen... aber irgendwie funtzt das nich - geht das überhaupt, oder geht das bei op[] gerade mal nicht so schön, wie bei dem rest?<br />
ich dachte, ich überlade den non-const und der für const macht eben nen const cast und ruf dann meinen op[] non-const auf, also so was hier in der richtung:</p>
<pre><code class="language-cpp">template &lt;typename T, typename const_reference, typename size_type&gt;
struct indexable
{
	const_reference operator[](size_type n) const
	{
		return (static_cast&lt;const T*&gt;(this)-&gt;operator[](n));
	}
};
</code></pre>
<p>aber als es so am anfang net geklappt hatte, hab ich dann einfach das aus boost 1:1 übernommen, ohne noch ma nachzudenken -.-</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1712412</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712412</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 18 May 2009 18:39:26 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 19:17:07 GMT]]></title><description><![CDATA[<p>für das was du versuchst zu tun ist indexable nicht gedacht. Es ist nur für iteratorartige Objekte gedacht, nicht für containerartige.<br />
Für die ewig gleichen const/non-const-implementierungen von Containerzugriffen weiß ich auch keinen gescheiten Rat. Wenn du was findest was die Arbeit abnimmt sag Bescheid, ich bin grad dabei ne std::vector-Implementierung zu schreiben, da gibts auch einen ganzen Stapel doppelte Accessor <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>PS: aber danke für den Irrtum, die Iterator-Operatoren werden im nächsten Artikel zu boost::operators besprochen, da kann ich gleich nochmal drauf hinweisen dass das nicht für Container da 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>
]]></description><link>https://www.c-plusplus.net/forum/post/1712431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712431</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 18 May 2009 19:17:07 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 19:41:03 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>PS: aber danke für den Irrtum, die Iterator-Operatoren werden im nächsten Artikel zu boost::operators besprochen, da kann ich gleich nochmal drauf hinweisen dass das nicht für Container da 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>
</blockquote>
<p>Nur deshalb hab ich das doch gepostet <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=";D"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1712448</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712448</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 18 May 2009 19:41:03 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 20:33:23 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/16305">@unskilled</a>,<br />
Ist dein Problem denn nun gelöst? Also du wolltest ja wohl sowas machen, oder?</p>
<pre><code class="language-cpp">#define MACRO_CREATE_OPERATOR_INDEXABLE(C, R, S) \
	R operator [](S index) \
	{ \
		return const_cast&lt;R&gt;((*static_cast&lt;C const*&gt;(this))[index]); \
	}

class Foo
{
	unsigned char data[4];

public:
	MACRO_CREATE_OPERATOR_INDEXABLE(Foo, unsigned char&amp;, std::size_t)

	unsigned char const&amp; operator [](std::size_t index) const
	{
		return data[index];
	}
};
</code></pre>
<p>Ja, Makros sind nicht so schön, aber es ist am einfachsten, so dass auch die Überladung funktioniert. Man bemerke übrigens, dass ich aus der nicht konstanten Version auf die konstante Version zugreife, ist etwas sicherer <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/1712482</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712482</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 18 May 2009 20:33:23 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 20:38:10 GMT]]></title><description><![CDATA[<p>und was bitte macht den Unterschied zwischen den Makro und einem einfach als Member definierten Operator? - Mal abgesehn davon dass das Makro eine Zeile mehr bei der Definition und eine zusätzliche Zeile für den Aufruf braucht (und am besten noch eine Zeile für den #undef) <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/1712491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712491</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 18 May 2009 20:38:10 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 20:40:56 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>und was bitte macht den Unterschied zwischen den Makro und einem einfach als Member definierten Operator? - Mal abgesehn davon dass das Makro eine Zeile mehr bei der Definition und eine zusätzliche Zeile für den Aufruf braucht (und am besten noch eine Zeile für den #undef) <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>
</blockquote>
<p>naja - es geht halt für jeden container - aber so richtig gefallen tuts mir auch nich -.-<br />
danke trotzdem, mal gucken, ob ichs nehm oder doch den gleichen code zweimal schreibe ^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1712494</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712494</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 18 May 2009 20:40:56 GMT</pubDate></item><item><title><![CDATA[Reply to (boost::) indexable on Mon, 18 May 2009 20:41:39 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>und was bitte macht den Unterschied zwischen den Makro und einem einfach als Member definierten Operator? - Mal abgesehn davon dass das Makro eine Zeile mehr bei der Definition und eine zusätzliche Zeile für den Aufruf braucht (und am besten noch eine Zeile für den #undef) <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>
</blockquote>
<p>Du kannst es wiederverwenden? Das Makro ist natürlich nicht für eine einzige Klasse gedacht, was allerdings das <code>indexable</code> wohl auch nicht gedacht 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>Wenn man die Sache als Klasse implementiert, hat man leider das Problem, dass die Überladung nicht hinhaut, weil es ein anderer Scope ist. Da müsste man gleich auch noch ein <code>using</code> hinschreiben, was die Sache gleich komplex macht. Deshalb das Makro statt einer Klasse, von welcher man ableiten kann.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1712496</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1712496</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Mon, 18 May 2009 20:41:39 GMT</pubDate></item></channel></rss>