Search code examples
c++stringiteratortokenizeboost-tokenizer

The "no known conversion" from const char* to an iterator error - another take


I'm doing the following:

using namespace boost;
const char* line = // ...
size_t line_length = // ...
// ...
tokenizer<escaped_list_separator<char> > line_tokenizer(
    line, line + line_length,
    escaped_list_separator<char>('\\', ',', '\"'));

Expecting to use the boost::tokenizer constructor

tokenizer(Iterator first, Iterator last,
          const TokenizerFunc& f = TokenizerFunc()) 
  : first_(first), last_(last), f_(f) { }

but GCC 4.9.3 gives me:

no known conversion for argument 1 from ‘const char*’ to ‘__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >’

Now, I've seen a couple of related questions in which the answer was forgetting to #include <algorithm> - but I have included it. Is there some other missing include, or is it another issue?


Solution

  • Since you're using boost, you can do:

    #include <boost/utility/string_ref.hpp>
    // ...
    const boost::string_ref line_(line, line_length);
    tokenizer<escaped_list_separator<char> > line_tokenizer(
        line_, escaped_list_separator<char>('\\', ',', '\"'));
    

    and that seems to work. Read more about string_ref and the other utilities here.

    Of course, if you have an implementation of the Guidelines Support Library, use string_span (a.k.a. string_view) from there (here's one implementation). It's even making it into the standard library probably.

    Update: string_view is in the C++ standard in C++17. Now you can write:

    #include <string_view>
    // ...
    std::string_view line_ { line, line_length };
    tokenizer<escaped_list_separator<char> > line_tokenizer(
        line_, escaped_list_separator<char>('\\', ',', '\"'));