views:

288

answers:

3

I'm having some trouble getting Boost Xpressive to work as I expect. I'm trying to split a line of text into fields delimited by tab characters:

wstring ws = L"Field1\tField2\tField3";

wsregex_token_iterator fieldIt(ws.begin(), ws.end(), as_xpr(L'\t'), -1);
wsregex_token_iterator endIt;

So far, so good; the above works fine. The problem comes when I try to initialize a vector using the range form of the constructor:

vector<wstring> fields(fieldIt, endIt);

The compiler complains:

cannot convert parameter 1 from 'boost::xpressive::wsregex_token_iterator' to 'unsigned int'

It appears that the compiler assumes that I'm trying to use the vector(size_type n, const T& t) form of the constructor rather than the template <class InputIterator> vector(InputIterator, InputIterator) form. I just can't figure out how to tell it otherwise. Any ideas?

oh: I'm using boost 1.34.1. And Visual Studio.net 2005.

A: 

Nevermind, it was a dumb copy/paste error. I forgot to define endIt before using it, so the compiler assumed it was an int. :P

+1  A: 

Ah, PEBKAC. This kind of stuff happens to me more than I'd like to admit.

EDIT:

Wait a minute, 'the compiler assumed it was an int'? No modern compiler is going to assume an unamed variable is an int. I'm sure I'm missing something here; but I'm confused. (Happens often enough! :)

Although, when dealing with template errors, compilers say all sorts of confusing things.

Bernard
+1  A: 

Bernard: You're right, it was a little more complicated than that. Not only did I not declare endIt before using it, I actually called it end, which at that scope turned out to be already defined as:

boost::range_iterator<remove_const<T>::type>

And I guess the compiler couldn't find a constructor for vector that took that kind of thing as a second argument. It was confusing, though, because the compiler was complaining about the first argument rather than the second and so the real culprit avoided scrutiny.

There's no better tonic for a PEBKAC problem than to rewrite your code to post as a sample, though. :)