title: Converting strings to pointers
body: <p>I'm trying to convert a pointer to string, and the string back to the pointer (lexical casting) using the following code. The conversion from pointer to string works fine, but not the other way round. </p>
  
  <p><strong>Why does this happen? 
   Is there some other way I can convert a string to a pointer?</strong></p>
  
  <p>I'm not worried about errors caused by incorrect format of the string. The string I'm trying to convert to a pointer will always be generated by converting a pointer to string.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>//TB_ConvertToString.cpp
  
  #include&lt;iostream&gt;
  #include&lt;cstdio&gt;
  #include&lt;sstream&gt;
  #include&lt;string&gt;
  
  //Functions to convert data types to and from strings   
  template &lt;typename T&gt; std::string ToString ( T Number )
  {
      std::stringstream ss;
      ss &lt;&lt; Number;
      return ss.str();
  };
  
  template &lt;typename T&gt; 
  T FromString ( const std::string &amp;Text )
  {
      std::stringstream ss(Text);
      T result;
      ss &gt;&gt; result;
      return result;
  }
  //-------------------------------------------------
  
  
  using namespace std;
  
  int main()
  {
      int a =10;  
      int* p1 = &amp;a;
      // this works------------
      string s1 = ToString&lt;int *&gt;(p1);
      printf("\n ptr: %s",s1.c_str());
      //----------------------
  
      //this gives compilation errors -----
      //int * p2 = FromString&lt;int *&gt;(s1);
      //printf("\n ptr: %p",p2);
      //----------------------
  
  
      cout&lt;&lt;"\n\n";
      return 0;
  };
  </code></pre>
  
  <p>Un-commenting the problem section gives the following compilation errors:</p>
  
  <pre><code>nehak@Marvin:~/Desktop$ g++ TB_ConvertToString.cpp 
  TB_ConvertToString.cpp: In function ‘T FromString(const string&amp;) [with T = int*, std::string = std::basic_string&lt;char&gt;]’:
  TB_ConvertToString.cpp:39:33:   instantiated from here
  TB_ConvertToString.cpp:21:2: error: no match for ‘operator&gt;&gt;’ in ‘ss &gt;&gt; result’
  TB_ConvertToString.cpp:21:2: note: candidates are:
  /usr/include/c++/4.6/istream:122:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; (*)(std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp;)) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:122:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::basic_istream&lt;char&gt;::__istream_type&amp; (*)(std::basic_istream&lt;char&gt;::__istream_type&amp;) {aka std::basic_istream&lt;char&gt;&amp; (*)(std::basic_istream&lt;char&gt;&amp;)}’
  /usr/include/c++/4.6/istream:126:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(std::basic_istream&lt;_CharT, _Traits&gt;::__ios_type&amp; (*)(std::basic_istream&lt;_CharT, _Traits&gt;::__ios_type&amp;)) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__ios_type = std::basic_ios&lt;char&gt;]
  /usr/include/c++/4.6/istream:126:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::basic_istream&lt;char&gt;::__ios_type&amp; (*)(std::basic_istream&lt;char&gt;::__ios_type&amp;) {aka std::basic_ios&lt;char&gt;&amp; (*)(std::basic_ios&lt;char&gt;&amp;)}’
  /usr/include/c++/4.6/istream:133:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(std::ios_base&amp; (*)(std::ios_base&amp;)) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:133:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::ios_base&amp; (*)(std::ios_base&amp;)’
  /usr/include/c++/4.6/istream:169:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(bool&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:169:7: note:   no known conversion for argument 1 from ‘int*’ to ‘bool&amp;’
  /usr/include/c++/4.6/istream:173:7: note: std::basic_istream&lt;_CharT, _Traits&gt;&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(short int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
  /usr/include/c++/4.6/istream:173:7: note:   no known conversion for argument 1 from ‘int*’ to ‘short int&amp;’
  /usr/include/c++/4.6/istream:176:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(short unsigned int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:176:7: note:   no known conversion for argument 1 from ‘int*’ to ‘short unsigned int&amp;’
  /usr/include/c++/4.6/istream:180:7: note: std::basic_istream&lt;_CharT, _Traits&gt;&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;]
  /usr/include/c++/4.6/istream:180:7: note:   no known conversion for argument 1 from ‘int*’ to ‘int&amp;’
  /usr/include/c++/4.6/istream:183:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(unsigned int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:183:7: note:   no known conversion for argument 1 from ‘int*’ to ‘unsigned int&amp;’
  /usr/include/c++/4.6/istream:187:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(long int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:187:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long int&amp;’
  /usr/include/c++/4.6/istream:191:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(long unsigned int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:191:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long unsigned int&amp;’
  /usr/include/c++/4.6/istream:196:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(long long int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:196:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long long int&amp;’
  /usr/include/c++/4.6/istream:200:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(long long unsigned int&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:200:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long long unsigned int&amp;’
  /usr/include/c++/4.6/istream:205:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(float&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:205:7: note:   no known conversion for argument 1 from ‘int*’ to ‘float&amp;’
  /usr/include/c++/4.6/istream:209:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(double&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:209:7: note:   no known conversion for argument 1 from ‘int*’ to ‘double&amp;’
  /usr/include/c++/4.6/istream:213:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(long double&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:213:7: note:   no known conversion for argument 1 from ‘int*’ to ‘long double&amp;’
  /usr/include/c++/4.6/istream:217:7: note: std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(void*&amp;) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__istream_type = std::basic_istream&lt;char&gt;]
  /usr/include/c++/4.6/istream:217:7: note:   no known conversion for argument 1 from ‘int*’ to ‘void*&amp;’
  /usr/include/c++/4.6/istream:241:7: note: std::basic_istream&lt;_CharT, _Traits&gt;&amp; std::basic_istream&lt;_CharT, _Traits&gt;::operator&gt;&gt;(std::basic_istream&lt;_CharT, _Traits&gt;::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, std::basic_istream&lt;_CharT, _Traits&gt;::__streambuf_type = std::basic_streambuf&lt;char&gt;]
  /usr/include/c++/4.6/istream:241:7: note:   no known conversion for argument 1 from ‘int*’ to ‘std::basic_istream&lt;char&gt;::__streambuf_type* {aka std::basic_streambuf&lt;char&gt;*}’
  /usr/include/c++/4.6/bits/basic_string.tcc:998:5: note: template&lt;class _CharT, class _Traits, class _Alloc&gt; std::basic_istream&lt;_CharT, _Traits&gt;&amp; std::operator&gt;&gt;(std::basic_istream&lt;_CharT, _Traits&gt;&amp;, std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&amp;)
  /usr/include/c++/4.6/bits/istream.tcc:957:5: note: template&lt;class _CharT2, class _Traits2&gt; std::basic_istream&lt;_CharT, _Traits&gt;&amp; std::operator&gt;&gt;(std::basic_istream&lt;_CharT, _Traits&gt;&amp;, _CharT2*)
  /usr/include/c++/4.6/bits/istream.tcc:925:5: note: template&lt;class _CharT, class _Traits&gt; std::basic_istream&lt;_CharT, _Traits&gt;&amp; std::operator&gt;&gt;(std::basic_istream&lt;_CharT, _Traits&gt;&amp;, _CharT&amp;)
  /usr/include/c++/4.6/istream:709:5: note: template&lt;class _Traits&gt; std::basic_istream&lt;char, _Traits&gt;&amp; std::operator&gt;&gt;(std::basic_istream&lt;char, _Traits&gt;&amp;, unsigned char&amp;)
  /usr/include/c++/4.6/istream:714:5: note: template&lt;class _Traits&gt; std::basic_istream&lt;char, _Traits&gt;&amp; std::operator&gt;&gt;(std::basic_istream&lt;char, _Traits&gt;&amp;, signed char&amp;)
  /usr/include/c++/4.6/istream:756:5: note: template&lt;class _Traits&gt; std::basic_istream&lt;char, _Traits&gt;&amp; std::operator&gt;&gt;(std::basic_istream&lt;char, _Traits&gt;&amp;, unsigned char*)
  /usr/include/c++/4.6/istream:761:5: note: template&lt;class _Traits&gt; std::basic_istream&lt;char, _Traits&gt;&amp; std::operator&gt;&gt;(std::basic_istream&lt;char, _Traits&gt;&amp;, signed char*)
  nehak@Marvin:~/Desktop$ 
  </code></pre>