Search code examples
c++booststdstringboost-iterators

In boost, how to pass boost iterator to a function somehow casted as std::string


See specific question as a comment at the end of the following code.

std::string s("my sample string \"with quotes\"");

boost::escaped_list_separator<char> 
els(""," ","\"\'");

boost::tokenizer<boost::escaped_list_separator<char> >::iterator 
itr;

boost::tokenizer<boost::escaped_list_separator<char> > 
tok(s, els);

itr=tok.begin();
if (itr!=tok.end()) 
    fn_that_receives_pointer_to_std_string(itr); // <---- IS IT POSSIBLE TO SEND POINTER AND NOT HAVE TO CREATE A NEW STRING ??

Solution

  • boost::tokenizer<boost::escaped_list_separator<char> >::iterator is not a pointer to std::string, but you can turn it into std::string const * with

    &(*itr)
    

    If a const pointer is not what you must pass, you may be able to do

    std::string s(*itr);
    

    and pass &s, depending on the ownership semantics of fn_that_receives_pointer_to_std_string. Boost Tokenizer does no distinguish between iterator and const_iterator, so the result of operator* is always const.