3

I have the following code that works in Visual Studio 10 and I want to port it on Linux -> GCC:

parse( v, srcUtf8.begin(), srcUtf8.end() ); <-- ERROR

picojson.h

template <typename Iter> inline std::string parse(value& out, Iter& pos, const Iter& last) 
{
 ...
}

I get this error:

error: no matching function for call to ‘parse(picojson::value&, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >)’

note: candidates are: std::string picojson::parse(picojson::value&, Iter&, const Iter&) [with Iter = __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]

Could I get please some help on this ?

cristian
  • 518
  • 5
  • 20

1 Answers1

2

The problem is that function parse has the second parameters of type Iter& pos that is a non-constant reference

template <typename Iter> inline std::string parse(value& out, Iter& pos, const Iter& last)

You may not bind a non-const reference to temporary object srcUtf8.begin()

Try the following

auto it = srcUtf8.begin();
parse( v, it, srcUtf8.end() ); 

Or instead of the type specifier auto write the explicit type of the iterator. For example if it is an iterator of class std::string then

std::string::iterator it = srcUtf8.begin();
parse( v, it, srcUtf8.end() ); 

Or if srcUtf8 is a constant object then

std::string::const_iterator it = srcUtf8.begin();
parse( v, it, srcUtf8.end() ); 

As for the MS VC++ 2010 then it has many bugs or its own language extensions that do not satisfy the C++ Standard.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    The reason you don't need this on VS is because of this [extension](http://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug) – NathanOliver Oct 26 '15 at 14:57
  • @Vlad thank you fro suggestion. Trying auto it = srcUtf8.begin(); I get this error: `Multiple markers at this line - cannot convert ‘__gnu_cxx::__normal_iterator, std::allocator > >’ to ‘int’ in initialization - ‘auto’ will change meaning in C++0x; please remove it - ISO C++ forbids declaration of ‘it’ with no type` – cristian Oct 26 '15 at 14:57
  • @cristian It seems you use a compiler that does not support C++ 2011. You should switch on the supporting. Otherwise write instead of the auto std::string::iterator – Vlad from Moscow Oct 26 '15 at 15:02
  • @Vlad, yes, I need to use GCC 4.4.7 or older, forgot to mention that. I have made the change and it's compiling error free. Thank you. – cristian Oct 26 '15 at 15:04
  • @NathanOliver: But VC does emit a warning if you set the appropriate warning level. Emitting a warning for ill-formed code is enough to satisy ISO-standard requirements, because the standard does not distinguish between warnings and errors (and you can make it an error with `/Za`). – Christian Hackl Oct 26 '15 at 15:13
  • @ChristianHackl Yes that is true but out of the box VS will happily compile the code where g++ or clang would not. http://coliru.stacked-crooked.com/a/e0ba946d8f5eca89 – NathanOliver Oct 26 '15 at 15:17
  • @NathanOliver: I know, but I don't think default compiler options are that important. You always have to specify a lot of options to invoke a compiler for your specific needs and to make it interpret C++ as strictly as possible. That's true for GCC as well, which will happily swallow `int f(int x) { int a[x]; return a[0]; }` without even a single diagnostic message if you don't pass appropriate compiler options. `-Wall`, `-Wextra` and `-Wpedantic` are not even enough to make it report the proprietary extension. – Christian Hackl Oct 26 '15 at 15:25