<?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[string to std::any&amp;lt;any type&amp;gt;]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>Vieleicht könnt ihr mir helfen oder habt einen Rat für mich.</p>
<p>Ich habe eine classe in der jeder beliebige Typ mit hilfe von any gespeichert<br />
werden kann.<br />
Ich möchte diese variablen reinitialisieren oder neu setzen über ein file<br />
welches geladen wird.</p>
<p>Ich dachte schon über literale.<br />
Es wäre möglich den Typ in dem file zu speichern ala, &quot;u&quot;,&quot;ll&quot;,&quot;b&quot;,etc..</p>
<p>Leider fällt mir nichts ein wie ich das realisieren könnte.</p>
<p>Könnte mir jemand bitte einen Denkanstoß geben?</p>
<p>Hier ein simpler code, um zu zeigen was es können soll.</p>
<p>Danke!</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;experimental/any&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

class cValue {
public:
	cValue(auto value) {
		anyVar = value;
	}

	void setValue(std::string value) {
		/* convert string anyhow into any type */
		// anyVar = newTypevalue;
	}

	std::string getValue() {
		std::string value = &quot;&quot;;
		/* convert any type anyhow into string */
		// value = std::experimental::fundamentals_v1::any_cast&lt;???&gt;(anyVar);
		return value;
	}
private:
	std::experimental::fundamentals_v1::any anyVar;
};

int main() {	
	std::string s = &quot;&quot;;

	cValue myLongValue(1L);
	cValue myBoolValue(true);

	std::cin &gt;&gt; s;
	/* read new value and overwrite long value */
	myLongValue.setValue(s);

	std::cin &gt;&gt; s;
	/* read new value and overwrite bool value */
	myBoolValue.setValue(s);

	/* need to be stored in a vector */
	std::vector&lt;cValue*&gt; v;
	v.push_back(&amp;myLongValue);
	v.push_back(&amp;myBoolValue);

	/* get string values from list */
	for(const auto&amp; value : v) {
		std::cout &lt;&lt; value-&gt;getValue() &lt;&lt; std::endl;
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/339849/string-to-std-any-lt-any-type-gt</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 17:00:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/339849.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 03 Oct 2016 17:49:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to string to std::any&amp;lt;any type&amp;gt; on Mon, 03 Oct 2016 17:49:17 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>Vieleicht könnt ihr mir helfen oder habt einen Rat für mich.</p>
<p>Ich habe eine classe in der jeder beliebige Typ mit hilfe von any gespeichert<br />
werden kann.<br />
Ich möchte diese variablen reinitialisieren oder neu setzen über ein file<br />
welches geladen wird.</p>
<p>Ich dachte schon über literale.<br />
Es wäre möglich den Typ in dem file zu speichern ala, &quot;u&quot;,&quot;ll&quot;,&quot;b&quot;,etc..</p>
<p>Leider fällt mir nichts ein wie ich das realisieren könnte.</p>
<p>Könnte mir jemand bitte einen Denkanstoß geben?</p>
<p>Hier ein simpler code, um zu zeigen was es können soll.</p>
<p>Danke!</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;experimental/any&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

class cValue {
public:
	cValue(auto value) {
		anyVar = value;
	}

	void setValue(std::string value) {
		/* convert string anyhow into any type */
		// anyVar = newTypevalue;
	}

	std::string getValue() {
		std::string value = &quot;&quot;;
		/* convert any type anyhow into string */
		// value = std::experimental::fundamentals_v1::any_cast&lt;???&gt;(anyVar);
		return value;
	}
private:
	std::experimental::fundamentals_v1::any anyVar;
};

int main() {	
	std::string s = &quot;&quot;;

	cValue myLongValue(1L);
	cValue myBoolValue(true);

	std::cin &gt;&gt; s;
	/* read new value and overwrite long value */
	myLongValue.setValue(s);

	std::cin &gt;&gt; s;
	/* read new value and overwrite bool value */
	myBoolValue.setValue(s);

	/* need to be stored in a vector */
	std::vector&lt;cValue*&gt; v;
	v.push_back(&amp;myLongValue);
	v.push_back(&amp;myBoolValue);

	/* get string values from list */
	for(const auto&amp; value : v) {
		std::cout &lt;&lt; value-&gt;getValue() &lt;&lt; std::endl;
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2510413</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2510413</guid><dc:creator><![CDATA[_lex]]></dc:creator><pubDate>Mon, 03 Oct 2016 17:49:17 GMT</pubDate></item><item><title><![CDATA[Reply to string to std::any&amp;lt;any type&amp;gt; on Mon, 03 Oct 2016 18:54:16 GMT]]></title><description><![CDATA[<p>Es ist schwer, dir zu helfen, weil wir nicht wissen, was genau du eigentlich mit deiner Klasse vorhast.</p>
<p>Wenn du einen bestimmten Wert aus einem File auslesen willst, dann lies es aus (<a href="http://en.cppreference.com/w/cpp/io/basic_ifstream" rel="nofollow">siehe hier</a>) und übergebe den geparsten Wert deiner <code>setValue()</code> -Methode.<br />
Oder - halt je nach Kontext - gleich einen Operator für die Klasse definieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2510419</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2510419</guid><dc:creator><![CDATA[12345]]></dc:creator><pubDate>Mon, 03 Oct 2016 18:54:16 GMT</pubDate></item><item><title><![CDATA[Reply to string to std::any&amp;lt;any type&amp;gt; on Wed, 05 Oct 2016 17:22:17 GMT]]></title><description><![CDATA[<p>Danke für die zahlreiche Unterstützung.<br />
Ich habe eine Lösung für mich gefunden.<br />
Allerdings mit ein paar Hässlichkeiten.<br />
Wer es kompiliert wird es schon sehen.</p>
<p>Falls jemand eine schönere Lösung hat, wäre ich sehr Dankbar darüber.</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;experimental/any&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

class cValue {
public:
	cValue(auto value) {
		anyVar = value;
	}

	void setValue(const std::string&amp; value, const std::string&amp; type) {
		if (type == &quot;l&quot;) {
			anyVar = std::stol(value);
		} else if (type == &quot;b&quot;) {
			anyVar = value != &quot;0&quot;;
		} else if (type == &quot;s&quot;)	{
			anyVar = value;
		} else {
			std::cout &lt;&lt; &quot;got unknow type\n&quot;;
		}
	}

	std::string getValue() {
		std::string type = anyVar.type().name();
		std::string value = &quot;&quot;;

		std::cout &lt;&lt; &quot;type: &quot; &lt;&lt; type &lt;&lt; '\n';

		if (type == &quot;l&quot;) {
			auto tmp = std::experimental::fundamentals_v1::any_cast&lt;long&gt;(anyVar);
			value = std::to_string(tmp);
		} else if (type == &quot;b&quot;) {
			auto tmp = std::experimental::fundamentals_v1::any_cast&lt;bool&gt;(anyVar);
			value = std::to_string(tmp);
		} else if (type == &quot;s&quot;) {
			value = std::experimental::fundamentals_v1::any_cast&lt;std::string&gt;(anyVar);
		} else {
			std::cout &lt;&lt; &quot;anyVar has unknown type\n&quot;;
		}

		return value;
	}
private:
	std::experimental::fundamentals_v1::any anyVar;
};

void getInput(std::string&amp; value, std::string&amp; type) {
	std::cin &gt;&gt; value;
	std::cin &gt;&gt; type;
}

int main() {	
	std::string value = &quot;&quot;;
	std::string type = &quot;&quot;;

	cValue myLongValue(1L);
	cValue myBoolValue(true);
	cValue myStringValue(std::string(&quot;empty&quot;));

	getInput(value, type);
	/* read new value and overwrite long value */
	myLongValue.setValue(value, type);

	getInput(value, type);
	/* read new value and overwrite bool value */
	myBoolValue.setValue(value, type);

	getInput(value, type);
	/* read new value and overwrite string value */
	myStringValue.setValue(value, type);

	/* need to be stored in a vector */
	std::vector&lt;cValue*&gt; v;
	v.push_back(&amp;myLongValue);
	v.push_back(&amp;myBoolValue);
	v.push_back(&amp;myStringValue);

	/* get string values from list */
	for(const auto&amp; value : v) {
		std::cout &lt;&lt; value-&gt;getValue() &lt;&lt; std::endl;
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2510679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2510679</guid><dc:creator><![CDATA[_lex]]></dc:creator><pubDate>Wed, 05 Oct 2016 17:22:17 GMT</pubDate></item><item><title><![CDATA[Reply to string to std::any&amp;lt;any type&amp;gt; on Thu, 06 Oct 2016 08:35:42 GMT]]></title><description><![CDATA[<p>_lex schrieb:</p>
<blockquote>
<p>Falls jemand eine schönere Lösung hat, wäre ich sehr Dankbar darüber.</p>
</blockquote>
<p>Da an der Schnittstelle der Klasse - also <code>setValue</code> und <code>getValue</code> - sowieso nur ein <code>std::string</code> vorliegt, solltest Du diesen auch als Member halten und auf das ganze any-Kram verzichten. Einzig der Konstruktor wäre zu überarbeiten:</p>
<pre><code>class cValue {
public:
    template&lt; typename T &gt;
    cValue(T value)
        : anyVar( std::to_string( value ) ) 
    {}

    void setValue(std::string value) {
        anyVar = value;
    }

    std::string getValue() const {
        return anyVar;
    }
private:
    std::string anyVar;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2510719</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2510719</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Thu, 06 Oct 2016 08:35:42 GMT</pubDate></item></channel></rss>