Note that there are some explanatory texts on larger screens.

plurals
  1. POC++. Get typename of a data as type, not as string
    text
    copied!<p>Before i start i will divide the problem into two parties: </p> <p>PART 1 :</p> <hr> <hr> <p>In c++ to get type of data we can use <code>typeid</code> but it's give you the data as <code>const char*</code> ,and i want it to return the type of the data.</p> <p>Example: </p> <pre><code>int data = 20 ; float data2 = 3.14 ; char *data3 = "hello world" ; std::cout&lt;&lt; typeid(data).nam() &lt;&lt; endl &lt;&lt; endl ; std::cout&lt;&lt; typeid(data2).nam() &lt;&lt; endl &lt;&lt; endl ; std::cout&lt;&lt; typeid(data3).nam() &lt;&lt; endl &lt;&lt; endl ; </code></pre> <p>Now i have a function that get data from void* , and convert it to another type :</p> <pre><code>template &lt;typename t &gt; void print (void *data ) { boost::any _t = static_cast&lt;t&gt; (data); cout &lt;&lt; boost::any_cast&lt;t&gt; (_t) &lt;&lt; endl &lt;&lt; endl; } </code></pre> <p>Now this works fine if you know your data type: Example: </p> <pre><code>void *mydata = alloca(size_object) ; void some_function_store_int_data_in_voidpointer( &amp;mydata) print &lt;int&gt; (mydata); // it's ok . </code></pre> <p>But this is impractical when you have lots of different datatypes, like this:</p> <pre><code>void somefunction(args &amp;a , void *dest ) { /*code returnd data */ } enum args { _INT_ , _FLOAT_ , _CHARPOINTER_ , }; vector &lt;void *test&gt; myvector ; myvector.resize (3) ; void somefunction(_INT_ , myvector.at(0) ) ; // store int in void* void somefunction(CHARPOINTER , myvector.at(0) ) ;// store char* in void* void somefunction(_FLOAT_ , myvector.at(0) ) ;// store float in void* print &lt;int&gt; (myvector.at(0)); print &lt;char*&gt; (myvector.at(1)); print &lt;float&gt; (myvector.at(2)); </code></pre> <p>1 - If i use something like this</p> <pre><code>print &lt;typeid(myvector.at(2))&gt; (myvector.at(2)); </code></pre> <p>i get an error because my data is float and I make it <code>const char*</code></p> <p>2 - Perhaps I can pass the type of every value if I have few data. This is OK. But what if I have 100 values from different types! </p> <p><strong>I am looking for something like: <code>typeid</code> but it' return the type not `const char*.</strong></p> <p>PART 2 </p> <hr> <hr> <p>because I have avector I will use a for_each algorithm like this:</p> <p><code>for_each ( myvector.begin() , myvector.end() , print&lt;/*what i should pass her int , float ,char* ...or what , */&gt;);</code></p> <p>In the previous code I can pass only one type to the function so the data from the same type will print. Else the data that are not the same type will print, but completely wrong (Strange format).</p> <p>So if I pass <code>char*</code> the <code>int</code> data will print completely wrong.</p> <p><strong>How can I do this differently?</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload