<?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 Strong Typedef für nicht-primitive Typen]]></title><description><![CDATA[<p>Hallo,</p>
<p>gibt es irgendwo so etwas wie <a href="http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/strong_typedef.html" rel="nofollow">http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/strong_typedef.html</a> für nicht-primitive Typen (Eigene Klassen, STL Container) ?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/303310/boost-strong-typedef-für-nicht-primitive-typen</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 13:51:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/303310.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 10 May 2012 16:04:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Boost Strong Typedef für nicht-primitive Typen on Thu, 10 May 2012 16:04:42 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>gibt es irgendwo so etwas wie <a href="http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/strong_typedef.html" rel="nofollow">http://www.boost.org/doc/libs/1_49_0/libs/serialization/doc/strong_typedef.html</a> für nicht-primitive Typen (Eigene Klassen, STL Container) ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2210239</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2210239</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Thu, 10 May 2012 16:04:42 GMT</pubDate></item><item><title><![CDATA[Reply to Boost Strong Typedef für nicht-primitive Typen on Thu, 10 May 2012 16:23:29 GMT]]></title><description><![CDATA[<p><s>Wie wäre es mit Boost? Ich glaube die haben ein strong typedef Makro im Serialization-Teil.</s></p>
<p>edit: Ich glaube ich hatte nicht genau verstanden, was du meinst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2210243</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2210243</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 10 May 2012 16:23:29 GMT</pubDate></item><item><title><![CDATA[Reply to Boost Strong Typedef für nicht-primitive Typen on Thu, 10 May 2012 16:37:26 GMT]]></title><description><![CDATA[<p>Hab mir jetzt selber was gebastelt: (Leider unterstützt noch kein Compiler inheriting constructors)</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;type_traits&gt;

template &lt;typename T, typename Id&gt;
struct StrongType : T {
	template &lt;typename Arg, typename ... Args&gt;
	StrongType(Arg&amp;&amp; arg, Args&amp;&amp;... args) : T(std::forward&lt;Arg&gt;(arg), std::forward&lt;Args&gt;(args)...) {}

	template &lt;typename V&gt;
	StrongType(std::initializer_list&lt;V&gt; init) : T(init) {}

	StrongType&amp; operator=(StrongType&amp; const other) {
		T::operator=(other);
	}
};

int main() {
	struct a__{};
	struct b__{};
	typedef StrongType&lt;std::vector&lt;int&gt;, a__&gt; a;
	typedef StrongType&lt;std::vector&lt;int&gt;, b__&gt; b;
	a avec = {1,2,3,4,5,6};
	b bvec = {1,2,3};
	std::cout &lt;&lt; avec.size() &lt;&lt; std::endl;
	avec = bvec;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2210244</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2210244</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Thu, 10 May 2012 16:37:26 GMT</pubDate></item><item><title><![CDATA[Reply to Boost Strong Typedef für nicht-primitive Typen on Thu, 10 May 2012 16:43:22 GMT]]></title><description><![CDATA[<p>Dieses hier sollte schon einmal ein Stück weiterhelfen. Das kann man dann auch insgesamt als Makro definieren, dann hat man die gleiche Syntax wie bei Boost:</p>
<pre><code class="language-cpp">#include &lt;vector&gt;

template &lt;typename T&gt; struct StrongTypedef : public T
{
  explicit StrongTypedef(const T&amp; t): T(t) {}
  StrongTypedef(): T() {};
  StrongTypedef&amp; operator=(const T &amp;rhs) { T::operator=(rhs); return *this;} 
  operator const T &amp; () const {return *this; }
  operator T &amp; () { return *this; } 
};

typedef StrongTypedef&lt;std::vector&lt;int&gt; &gt; a;

void f(std::vector&lt;int&gt; x);  // (1) function to handle simple integers
void f(a x);    // (2) special function to handle integers of type a 
int main(){
  std::vector&lt;int&gt; x(1);
  a y;
  y = x;      // other operations permitted as a is converted as necessary
  f(x);       // chooses (1)
  f(y);       // chooses (2)
  y.push_back(1);
}
</code></pre>
<p>Das sollte funktionieren, solange die Implmentierung in einer abgeleitete Klasse ohne eigene Member keinen zusätzlichen Speicher belegt. Ansonsten stirbt man am möglicherweise nicht-virtuellen Destruktor der Basisklasse. Und man hätte nun noch die wunderschöne Aufgabe, per Templatemagie alle existierenden Operatoren der Basisklasse auch auf die Typedefklasse zu übertragen.</p>
<p>Und vermutlich habe ich irgendwas wichtiges übersehen, da ich nach meiner ersten. falschen Antwort dies hier so hopplahop zusammengeschrieben habe.</p>
<p>edit: Jetzt warst du selber schneller mit der gleichen Idee. In C++11 geht's natürlich eine Nummer schöner, wie du schon siehst.<br />
edit2: Dir fehlt noch ein return bei der Zuweisung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2210246</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2210246</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 10 May 2012 16:43:22 GMT</pubDate></item><item><title><![CDATA[Reply to Boost Strong Typedef für nicht-primitive Typen on Thu, 10 May 2012 16:54:47 GMT]]></title><description><![CDATA[<p>Ja, das return hat mein Compiler mir auch schon gemeldet. <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="😃"
    /> Außerdem hatte ich noch einen Schreibfehler:</p>
<pre><code class="language-cpp">StrongType&amp; const
</code></pre>
<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="😃"
    /></p>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Und man hätte nun noch die wunderschöne Aufgabe, per Templatemagie alle existierenden Operatoren der Basisklasse auch auf die Typedefklasse zu übertragen.</p>
</blockquote>
<p>Mit Boost.Preprocessor sollte das gehen, dazu habe ich im Moment keine Lust <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="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2210248</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2210248</guid><dc:creator><![CDATA[pyhax]]></dc:creator><pubDate>Thu, 10 May 2012 16:54:47 GMT</pubDate></item></channel></rss>