<?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[bei typedefs trotz gleicher Basis Compilerfehler erzwingen]]></title><description><![CDATA[<p>Kennt jemand eine Möglichkeit mit der man bei typedefs, die letztlich auf denselben Typ verweisen einen Fehler bekommt, wenn der Typ nicht dem &quot;speziellen typedef&quot; entspricht.</p>
<p>Ein Beispiel sagt mehr als 1000 Worte:</p>
<pre><code class="language-cpp">typedef A A1;
typedef A A2;

void foo( A1 ) {}

int main() {
 A2 a2;
 foo( a2 ); //--&gt; hier soll der Compiler einen Fehler melden
}
</code></pre>
<p>Ich dachte zuerst an BOOST_STRONG_TYPEDEF, der aber nur ambigous calls korrekt auflöst:</p>
<pre><code class="language-cpp">BOOST_STRONG_TYPEDEF( A, A1 );
BOOST_STRONG_TYPEDEF( A, A2 );

void foo( A1 ) {}
void foo( A2 ) {}

int main() {
 A1 a1;
 foo( a1 ); //--&gt; foo( A1 )
 A2 a2;
 foo( a2 ); //--&gt; foo( A2 )
}
</code></pre>
<p>Allerdings erhält man keinen Compilerfehler, wenn im obigen Beispiel foo für A2 nicht definiert ist, da dann automatisch foo( A1 ) verwendet werden kann. Was im Normalfall ja auch genau so sein soll.</p>
<p>In dem speziellen Fall möchte ich aber gerne genau dieses Verhalten vermeiden. Mir war so, als hätte irgendwo in boost ein Makro gesehen, dass sowas behandelt.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/289899/bei-typedefs-trotz-gleicher-basis-compilerfehler-erzwingen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 19:44:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/289899.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 15 Jul 2011 06:38:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to bei typedefs trotz gleicher Basis Compilerfehler erzwingen on Fri, 15 Jul 2011 06:38:34 GMT]]></title><description><![CDATA[<p>Kennt jemand eine Möglichkeit mit der man bei typedefs, die letztlich auf denselben Typ verweisen einen Fehler bekommt, wenn der Typ nicht dem &quot;speziellen typedef&quot; entspricht.</p>
<p>Ein Beispiel sagt mehr als 1000 Worte:</p>
<pre><code class="language-cpp">typedef A A1;
typedef A A2;

void foo( A1 ) {}

int main() {
 A2 a2;
 foo( a2 ); //--&gt; hier soll der Compiler einen Fehler melden
}
</code></pre>
<p>Ich dachte zuerst an BOOST_STRONG_TYPEDEF, der aber nur ambigous calls korrekt auflöst:</p>
<pre><code class="language-cpp">BOOST_STRONG_TYPEDEF( A, A1 );
BOOST_STRONG_TYPEDEF( A, A2 );

void foo( A1 ) {}
void foo( A2 ) {}

int main() {
 A1 a1;
 foo( a1 ); //--&gt; foo( A1 )
 A2 a2;
 foo( a2 ); //--&gt; foo( A2 )
}
</code></pre>
<p>Allerdings erhält man keinen Compilerfehler, wenn im obigen Beispiel foo für A2 nicht definiert ist, da dann automatisch foo( A1 ) verwendet werden kann. Was im Normalfall ja auch genau so sein soll.</p>
<p>In dem speziellen Fall möchte ich aber gerne genau dieses Verhalten vermeiden. Mir war so, als hätte irgendwo in boost ein Makro gesehen, dass sowas behandelt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2092963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2092963</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Fri, 15 Jul 2011 06:38:34 GMT</pubDate></item><item><title><![CDATA[Reply to bei typedefs trotz gleicher Basis Compilerfehler erzwingen on Fri, 15 Jul 2011 10:35:01 GMT]]></title><description><![CDATA[<p>Interessante Frage. Da ein typedef ja nur ein Alias und kein neuer Typ ist, fällt mir nicht ein, wie man das lösen könnte, außer halt so, dass man einen eigenen Typ draus macht, was aber vermutlich nicht das ist, was willst, weil das dann nicht mehr mit Basistypen geht.</p>
<pre><code class="language-cpp">struct A1 : A {};
struct A2 : A {};

void foo( A1 a1 ) {}

int main() {
	A2 a2;
	foo( a2 ); //--&gt; hier soll der Compiler einen Fehler melden
}
</code></pre>
<p>Edit: Oder suchst du das hier? <a href="http://drdobbs.com/184401633" rel="nofollow">http://drdobbs.com/184401633</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093052</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093052</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Fri, 15 Jul 2011 10:35:01 GMT</pubDate></item><item><title><![CDATA[Reply to bei typedefs trotz gleicher Basis Compilerfehler erzwingen on Fri, 15 Jul 2011 10:56:19 GMT]]></title><description><![CDATA[<p>Ich habe mir extra dafür mal vor Jahren diese Headedatei &quot;type.h&quot; angelegt:</p>
<pre><code class="language-cpp">#ifndef TYPE_H
#define TYPE_H

#ifdef TEST_TYPE

template&lt;typename T, class NAME&gt; class type
{
public:
	typedef type&lt;T, NAME&gt; THIS;

	type() {}
	type(const THIS&amp; t) { x = t.x; }
	type(const T&amp; _t) { x = _t; }

	T value() const { return x; }
	template&lt;class U&gt; U cast() const { return (U)x; }

	THIS&amp; operator  = (const THIS&amp; t) { x  = t.x; return *this; }
	THIS&amp; operator *= (const THIS&amp; t) { x *= t.x; return *this; }
	THIS&amp; operator /= (const THIS&amp; t) { x /= t.x; return *this; }
	THIS&amp; operator %= (const THIS&amp; t) { x %= t.x; return *this; }
	THIS&amp; operator += (const THIS&amp; t) { x += t.x; return *this; }
	THIS&amp; operator -= (const THIS&amp; t) { x -= t.x; return *this; }
	THIS&amp; operator &lt;&lt;=(const THIS&amp; t) { x &lt;&lt;= t.x; return *this; }
	THIS&amp; operator &gt;&gt;=(const THIS&amp; t) { x &gt;&gt;= t.x; return *this; }
	THIS&amp; operator &amp;= (const THIS&amp; t) { x &amp;= t.x; return *this; }
	THIS&amp; operator |= (const THIS&amp; t) { x |= t.x; return *this; }
	THIS&amp; operator ^= (const THIS&amp; t) { x ~= t.x; return *this; }

	const THIS&amp; operator ++ () { ++x; return *this; }
	const THIS&amp; operator -- () { --x; return *this }
	THIS operator ++ (int) { THIS t = *this; ++x; return t; }
	THIS operator -- (int) { THIS t = *this; --x; return t; }

	bool operator ==(const THIS&amp; t) const { return x == t.x; }
	bool operator !=(const THIS&amp; t) const { return x != t.x; }
	bool operator &lt; (const THIS&amp; t) const { return x &lt;  t.x; }
	bool operator &lt;=(const THIS&amp; t) const { return x &lt;= t.x; }
	bool operator &gt; (const THIS&amp; t) const { return x &gt;  t.x; }
	bool operator &gt;=(const THIS&amp; t) const { return x &gt;= t.x; }

	THIS operator * (const THIS&amp; t) const { return THIS(x * t.x); }
	THIS operator / (const THIS&amp; t) const { return THIS(x / t.x); }
	THIS operator % (const THIS&amp; t) const { return THIS(x % t.x); }
	THIS operator + (const THIS&amp; t) const { return THIS(x + t.x); }
	THIS operator - (const THIS&amp; t) const { return THIS(x - t.x); }
	THIS operator &lt;&lt;(const THIS&amp; t) const { return THIS(x &lt;&lt; t.x); }
	THIS operator &gt;&gt;(const THIS&amp; t) const { return THIS(x &gt;&gt; t.x); }
	THIS operator &amp; (const THIS&amp; t) const { return THIS(x &amp; t.x); }
	THIS operator | (const THIS&amp; t) const { return THIS(x | t.x); }
	THIS operator ^ (const THIS&amp; t) const { return THIS(x ^ t.x); }
	THIS operator + () const { return THIS( x); }
	THIS operator - () const { return THIS(-x); }
	THIS operator ! () const { return THIS(!x); }
	THIS operator ~ () const { return THIS(~x); }

	friend bool operator ==(const T&amp; _t, const THIS&amp; t) { return _t == t.x; }
	friend bool operator !=(const T&amp; _t, const THIS&amp; t) { return _t != t.x; }
	friend bool operator &lt; (const T&amp; _t, const THIS&amp; t) { return _t &lt;  t.x; }
	friend bool operator &lt;=(const T&amp; _t, const THIS&amp; t) { return _t &lt;= t.x; }
	friend bool operator &gt; (const T&amp; _t, const THIS&amp; t) { return _t &gt;  t.x; }
	friend bool operator &gt;=(const T&amp; _t, const THIS&amp; t) { return _t &gt;= t.x; }

	friend THIS operator * (const T&amp; _t, const THIS&amp; t) { return THIS(_t * t.x); }
	friend THIS operator / (const T&amp; _t, const THIS&amp; t) { return THIS(_t / t.x); }
	friend THIS operator % (const T&amp; _t, const THIS&amp; t) { return THIS(_t % t.x); }
	friend THIS operator + (const T&amp; _t, const THIS&amp; t) { return THIS(_t + t.x); }
	friend THIS operator - (const T&amp; _t, const THIS&amp; t) { return THIS(_t - t.x); }
	friend THIS operator &lt;&lt;(const T&amp; _t, const THIS&amp; t) { return THIS(_t &lt;&lt; t.x); }
	friend THIS operator &gt;&gt;(const T&amp; _t, const THIS&amp; t) { return THIS(_t &gt;&gt; t.x); }
	friend THIS operator &amp; (const T&amp; _t, const THIS&amp; t) { return THIS(_t &amp; t.x); }
	friend THIS operator | (const T&amp; _t, const THIS&amp; t) { return THIS(_t | t.x); }
	friend THIS operator ^ (const T&amp; _t, const THIS&amp; t) { return THIS(_t ^ t.x); }

private:
	T x;
};

#define TYPEDEF(T, NAME)\
	struct _S_##NAME\
	{\
		static const char * const name() { return #NAME; }\
	};\
	typedef type&lt;T, _S_##NAME&gt; NAME;\
	/* partial template */\
	template&lt;class U&gt; U CAST(const NAME&amp; t)\
	{\
		return U(t.value()); /* t.template cast&lt;U&gt;(); */\
	}

#else

#define TYPEDEF(T, NAME)\
	typedef T NAME;

template&lt;class U, class T&gt; U CAST(const T&amp; t)
{
	return U(t);
};

#endif

#endif
</code></pre>
<p>Also anstatt typedef dann das Makro TYPEDEF benutzen:</p>
<pre><code class="language-cpp">#define TEST_TYPE
#include &quot;type.h&quot;

TYPEDEF(A, A1)
TYPEDEF(A, A2);
</code></pre>
<p>(mittels des Makros &quot;TEST_TYPE&quot; kann diese Überprüfung dann aktiviert werden, z.B. nur im Debug-Modus)</p>
<p>Und das Makro CAST(...) gibt es extra für den Fall, daß man dann doch explizit zwischen den Datentypen casten will.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093064</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093064</guid><dc:creator><![CDATA[Th69]]></dc:creator><pubDate>Fri, 15 Jul 2011 10:56:19 GMT</pubDate></item><item><title><![CDATA[Reply to bei typedefs trotz gleicher Basis Compilerfehler erzwingen on Fri, 15 Jul 2011 11:02:27 GMT]]></title><description><![CDATA[<p>n1 Th69!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093067</guid><dc:creator><![CDATA[Brilliant!]]></dc:creator><pubDate>Fri, 15 Jul 2011 11:02:27 GMT</pubDate></item><item><title><![CDATA[Reply to bei typedefs trotz gleicher Basis Compilerfehler erzwingen on Sun, 17 Jul 2011 17:34:35 GMT]]></title><description><![CDATA[<p>hatte irgendwie gehofft, dass es anders geht, aber dann eben auf die harte Tour! Danke Th69</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093961</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Sun, 17 Jul 2011 17:34:35 GMT</pubDate></item><item><title><![CDATA[Reply to bei typedefs trotz gleicher Basis Compilerfehler erzwingen on Sun, 17 Jul 2011 17:35:25 GMT]]></title><description><![CDATA[<p>hatte irgendwie gehofft, dass es anders geht, aber dann eben auf die harte Tour! Danke Th69</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2093963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2093963</guid><dc:creator><![CDATA[muffmolch]]></dc:creator><pubDate>Sun, 17 Jul 2011 17:35:25 GMT</pubDate></item></channel></rss>