4

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 ??
user497804
  • 292
  • 2
  • 7

3 Answers3

2

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

*itr will actually return a basic_string instead of a string, so you need to convert one to another:

using namespace std;
using namespace boost;

void fn_that_receives_pointer_to_std_string(string* str)
{
    cout << "str: " << *str << endl;
}

int main()
{
   string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3";
   tokenizer<escaped_list_separator<char> > tok(s);
   for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg)
   {   
       string tmp(*beg);
       fn_that_receives_pointer_to_std_string(&tmp);
   }   
}

I don't like the idea to passing the memory address of a string to another function. Consider passing it by copy or by reference.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
0

Sorry, it's impossible.

It's exactly the reason why the rule "take string parameters as std::string" is wrong. boost::iterator_range<const char*> can be better when a template is inappropriate (separate compilation for example).

Community
  • 1
  • 1
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220