<?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[Gleiche Template-Spezialisierung für const und non-const Funktion]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>muss ich in folgendem Code zwei verschiedene Versionen für die const und non-const getter schreiben oder gibt es einen Weg, dass es auf beide matcht?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct foo1{
	int bar() const{ return 1;};
};

struct foo2{
	int bar;
};

struct foo3{
	std::string bar(){ return &quot;&quot;;};
};

struct foo4{
	std::string bar;
};

template&lt;typename T&gt;
struct field_type;

template&lt;typename R, typename S&gt;
struct field_type&lt;R (S::*)&gt;{
	typedef R type;
};

template&lt;typename R, typename S&gt;
struct field_type&lt;R (S::*)() const&gt;{
	typedef R type;
};

/*template&lt;typename R, typename S&gt;
struct field_type&lt;R (S::*)()&gt;{
	typedef R type;
};*/

template&lt;typename U, typename V, typename W&gt;
struct field_info_type;

template&lt;typename U, typename V&gt;
struct field_info_type&lt;U, V, V (U::*)&gt; {
	static const int type = 2;
};

template&lt;typename U, typename V&gt;
struct field_info_type&lt;U, V, V (U::*)() const&gt; {
	static const int type = 1;
};

/*template&lt;typename U, typename V&gt;
struct field_info_type&lt;U, V, V (U::*)()&gt; {
	static const int type = 1;
};*/

int main(){
	std::cout &lt;&lt; field_info_type&lt;foo1, field_type&lt;decltype(&amp;foo1::bar)&gt;::type, decltype(&amp;foo1::bar)&gt;::type &lt;&lt; std::endl;
	std::cout &lt;&lt; field_info_type&lt;foo2, field_type&lt;decltype(&amp;foo2::bar)&gt;::type, decltype(&amp;foo2::bar)&gt;::type &lt;&lt; std::endl;
	std::cout &lt;&lt; field_info_type&lt;foo3, field_type&lt;decltype(&amp;foo3::bar)&gt;::type, decltype(&amp;foo3::bar)&gt;::type &lt;&lt; std::endl;
	std::cout &lt;&lt; field_info_type&lt;foo4, field_type&lt;decltype(&amp;foo4::bar)&gt;::type, decltype(&amp;foo4::bar)&gt;::type &lt;&lt; std::endl;
}
</code></pre>
<p>Ich möchte an den Zeilen in der main nichts ändern, da ich den Code später durch ein Makro generieren lassen möchte. (Oder halt einheitlich ändern)</p>
<p>Was ich aber absolut nicht verstehe:<br />
Warum ist die Ausgabe von dem Code?</p>
<blockquote>
<p>1<br />
2<br />
2<br />
2</p>
</blockquote>
<p>Also ich hätte einen Compilerfehler erwartet... Aber der nicht konstante getter passt in meine Funktion für den Member-Pointer?<br />
Warum das?</p>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/309321/gleiche-template-spezialisierung-für-const-und-non-const-funktion</link><generator>RSS for Node</generator><lastBuildDate>Wed, 05 Aug 2026 06:36:55 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/309321.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 19 Oct 2012 17:32:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Gleiche Template-Spezialisierung für const und non-const Funktion on Fri, 19 Oct 2012 17:32:52 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>muss ich in folgendem Code zwei verschiedene Versionen für die const und non-const getter schreiben oder gibt es einen Weg, dass es auf beide matcht?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct foo1{
	int bar() const{ return 1;};
};

struct foo2{
	int bar;
};

struct foo3{
	std::string bar(){ return &quot;&quot;;};
};

struct foo4{
	std::string bar;
};

template&lt;typename T&gt;
struct field_type;

template&lt;typename R, typename S&gt;
struct field_type&lt;R (S::*)&gt;{
	typedef R type;
};

template&lt;typename R, typename S&gt;
struct field_type&lt;R (S::*)() const&gt;{
	typedef R type;
};

/*template&lt;typename R, typename S&gt;
struct field_type&lt;R (S::*)()&gt;{
	typedef R type;
};*/

template&lt;typename U, typename V, typename W&gt;
struct field_info_type;

template&lt;typename U, typename V&gt;
struct field_info_type&lt;U, V, V (U::*)&gt; {
	static const int type = 2;
};

template&lt;typename U, typename V&gt;
struct field_info_type&lt;U, V, V (U::*)() const&gt; {
	static const int type = 1;
};

/*template&lt;typename U, typename V&gt;
struct field_info_type&lt;U, V, V (U::*)()&gt; {
	static const int type = 1;
};*/

int main(){
	std::cout &lt;&lt; field_info_type&lt;foo1, field_type&lt;decltype(&amp;foo1::bar)&gt;::type, decltype(&amp;foo1::bar)&gt;::type &lt;&lt; std::endl;
	std::cout &lt;&lt; field_info_type&lt;foo2, field_type&lt;decltype(&amp;foo2::bar)&gt;::type, decltype(&amp;foo2::bar)&gt;::type &lt;&lt; std::endl;
	std::cout &lt;&lt; field_info_type&lt;foo3, field_type&lt;decltype(&amp;foo3::bar)&gt;::type, decltype(&amp;foo3::bar)&gt;::type &lt;&lt; std::endl;
	std::cout &lt;&lt; field_info_type&lt;foo4, field_type&lt;decltype(&amp;foo4::bar)&gt;::type, decltype(&amp;foo4::bar)&gt;::type &lt;&lt; std::endl;
}
</code></pre>
<p>Ich möchte an den Zeilen in der main nichts ändern, da ich den Code später durch ein Makro generieren lassen möchte. (Oder halt einheitlich ändern)</p>
<p>Was ich aber absolut nicht verstehe:<br />
Warum ist die Ausgabe von dem Code?</p>
<blockquote>
<p>1<br />
2<br />
2<br />
2</p>
</blockquote>
<p>Also ich hätte einen Compilerfehler erwartet... Aber der nicht konstante getter passt in meine Funktion für den Member-Pointer?<br />
Warum das?</p>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262131</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Fri, 19 Oct 2012 17:32:52 GMT</pubDate></item><item><title><![CDATA[Reply to Gleiche Template-Spezialisierung für const und non-const Funktion on Fri, 19 Oct 2012 17:50:15 GMT]]></title><description><![CDATA[<p>XSpille schrieb:</p>
<blockquote>
<p>Aber der nicht konstante getter passt in meine Funktion für den Member-Pointer?</p>
</blockquote>
<p>Der konstante Getter tut das auch, nur hast dem Compiler ja eine Spezialisierung dafür gegeben, so dass das nicht auffällt.</p>
<p>Wird ein Templateparameter der Form</p>
<pre><code class="language-cpp">T U::*
</code></pre>
<p>mit einem Memberfunktionszeiger gefüttert, so wird T als entsprechender Funktionstyp deduziert.</p>
<pre><code class="language-cpp">field_type&lt;decltype(&amp;foo1::bar)&gt;::type // wäre int() const, wenn es die Spezialisierung für const-Memberfunktionen nicht gäbe
    field_type&lt;decltype(&amp;foo2::bar)&gt;::type // ist int
    field_type&lt;decltype(&amp;foo3::bar)&gt;::type // ist std::string()
    field_type&lt;decltype(&amp;foo4::bar)&gt;::type // ist std::string
</code></pre>
<p>Ältere Compiler sind dafür bekannt, diese Deduktion nicht immer korrekt durchzuführen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262134</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262134</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 19 Oct 2012 17:50:15 GMT</pubDate></item><item><title><![CDATA[Reply to Gleiche Template-Spezialisierung für const und non-const Funktion on Fri, 19 Oct 2012 19:20:44 GMT]]></title><description><![CDATA[<p>Danke Camper <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>eine Folgefrage habe ich noch:<br />
Wie nennt man die Rückgabe von foo3? Also ich meine das std::string()<br />
Wenns geht auch auf englisch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262165</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262165</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Fri, 19 Oct 2012 19:20:44 GMT</pubDate></item><item><title><![CDATA[Reply to Gleiche Template-Spezialisierung für const und non-const Funktion on Fri, 19 Oct 2012 19:31:06 GMT]]></title><description><![CDATA[<p>function (taking no arguments) returning std::string?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262168</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262168</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 19 Oct 2012 19:31:06 GMT</pubDate></item><item><title><![CDATA[Reply to Gleiche Template-Spezialisierung für const und non-const Funktion on Fri, 19 Oct 2012 19:34:19 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>function (taking no arguments) returning std::string?</p>
</blockquote>
<p>Ok... Ich dachte da gibts nen speziellen Ausdruck <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262171</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262171</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Fri, 19 Oct 2012 19:34:19 GMT</pubDate></item><item><title><![CDATA[Reply to Gleiche Template-Spezialisierung für const und non-const Funktion on Sat, 20 Oct 2012 00:39:04 GMT]]></title><description><![CDATA[<p>nullary function?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2262217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2262217</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Sat, 20 Oct 2012 00:39:04 GMT</pubDate></item></channel></rss>