title: Why do a C++ regular expression code that works with "cmatch" raises an exception with "smatch"?
body: <p>I am new to C++ regular expressions and cannot make them work with strings instead of char*. The examples I have seen so far were always for c strings. </p>
  
  <p>My real program, which I will not even try to show here, uses sub matches but I could not
  make them work, so I tried to modify a very simple working example but it does not work either. I use Visual Studio 2010 Ultimate.  </p>
  
  <p>The original - working - code:</p>
  
  <pre><code>const char *first = "abcd"; 
  const char *last = first + strlen(first); 
  std::cmatch mr; 
  std::regex rx("abc"); 
  std::regex_constants::match_flag_type fl = std::regex_constants::match_default;
  
  std::cout &lt;&lt; "search(f, l, \"abc\") == " &lt;&lt; std::boolalpha 
            &lt;&lt; regex_search(first, last, mr, rx) &lt;&lt; std::endl; 
  std::cout &lt;&lt; "  matched: \"" &lt;&lt; mr.str() &lt;&lt; "\"" &lt;&lt; std::endl; 
  
  std::cout &lt;&lt; "search(\"xabcd\", \"abc\") == " &lt;&lt; std::boolalpha
            &lt;&lt; regex_search("xabcd", mr, rx) &lt;&lt; std::endl; 
  std::cout &lt;&lt; "  matched: \"" &lt;&lt; mr.str() &lt;&lt; "\"" &lt;&lt; std::endl;
  </code></pre>
  
  <p>The modified code:</p>
  
  <pre><code>const string first = "abcd";     // char * =&gt; string
  std::smatch mr;                  // cmatch =&gt; smatch
  std::regex rx(string("abc")); 
  std::regex_constants::match_flag_type fl = std::regex_constants::match_default;
  
                 // this works:
  std::cout &lt;&lt; "search(f, l, \"abc\") == " &lt;&lt; std::boolalpha 
            &lt;&lt; regex_search(first, mr, rx) &lt;&lt; std::endl; 
  std::cout &lt;&lt; "  matched: \"" &lt;&lt; mr.str() &lt;&lt; "\"" &lt;&lt; std::endl; 
  
                 // after the next line executes mr seems good to me:
                 // mr[0] = {3, matched:true, first="abcd", second="d",...}
  std::cout &lt;&lt; "search(\"xabcd\", \"abc\") == " &lt;&lt; std::boolalpha
            &lt;&lt; regex_search(string("xabcd"), mr, rx) &lt;&lt; std::endl; 
                 // but the following line gives the error
                 // "Debug assertion failed"
                 // Expression: string iterators incompatible
  std::cout &lt;&lt; "  matched: \"" &lt;&lt; mr.str() &lt;&lt; "\"" &lt;&lt; std::endl;
  </code></pre>
  
  <p>Strange that one part of the modified code works, while the next part causes an exception. I even tried to use mr[0].str() but I got the same error message. Could you help me to solve this problem? </p>