Search code examples
c++templatesmost-vexing-parserapidjson

Conversion from 'Type (__cdecl *)(std::istream)' to 'Type &'


I have problems to understand the cause of an error when calling a template function in C++. The function in question is part from rapidjson and the definition is like:

template <unsigned parseFlags, typename InputStream, typename Handler>
    ParseResult Parse(InputStream& is, Handler& handler) 

My calling code is:

#include <iostream>
#include <sstream>
#include <fstream>
#include "rapidjson/rapidjson.h"
#include "rapidjson/reader.h"

class IStreamWrapper {
public:
  typedef char Ch;
  IStreamWrapper(std::istream& is) : is_(is) {
  }
  Ch Peek() const { // 1
    int c = is_.peek();
    return c == std::char_traits<char>::eof() ? '\0' : (Ch)c;
  }
  Ch Take() { // 2
    int c = is_.get();
    return c == std::char_traits<char>::eof() ? '\0' : (Ch)c;
  }
  size_t Tell() const { return (size_t)is_.tellg(); } // 3
  Ch* PutBegin() { assert(false); return 0; }
  void Put(Ch) { assert(false); }
  void Flush() { assert(false); }
  size_t PutEnd(Ch*) { assert(false); return 0; }
private:
  IStreamWrapper(const IStreamWrapper&);
  IStreamWrapper& operator=(const IStreamWrapper&);
  std::istream& is_;
};

struct EmptyHandler {
  bool Null() { return true; }
  bool Bool(bool b) { return true; }
  bool Int(int i) { return true; }
  bool Uint(unsigned u) { return true; }
  bool Int64(int64_t i) { return true; }
  bool Uint64(uint64_t u) { return true; }
  bool Double(double d) { return true; }
  bool String(const char* str, rapidjson::SizeType length, bool copy) { return true; }
  bool StartObject() { return true; }
  bool Key(const char* str, rapidjson::SizeType length, bool copy) { return true; }
  bool EndObject(rapidjson::SizeType memberCount) { return true; }
  bool StartArray() { return true; }
  bool EndArray(rapidjson::SizeType elementCount) { return true; }
};

int main(int argc, char*argv[]){
  std::ifstream input("example.json");
  IStreamWrapper is(std::istream(input));
  EmptyHandler handler;
  rapidjson::Reader reader;
  reader.Parse<rapidjson::kParseDefaultFlags, IStreamWrapper, EmptyHandler>(is, handler);
  return 0;
}

But MS Visual Studio 2013 is showing me the error:

error C2664: 'rapidjson::ParseResult rapidjson::GenericReader<rapidjson::UTF8<char>,rapidjson::UTF8<char>,rapidjson::CrtAllocator>::Parse<0,IStreamWrapper,EmptyHandler>(InputStream &,Handler &)' : cannot convert argument 1 from 'IStreamWrapper (__cdecl *)(std::istream)' to 'IStreamWrapper &'

In my understanding the types used for instantiation are exactly the same as passed to the template function. This code is just to demonstrate the problem - in my original code the istream is not from an file - so suggesting a different reading class for files from rapidjson would not solve my problem. Also I want to understand, why exactly this error is occurring here.

Any ideas?


Solution

  • IStreamWrapper is(std::istream(input));
    

    is equivalent to

    IStreamWrapper is(std::istream input);
    

    i.e., it declares a function called is taking a parameter of type std::istream named input and returning an IStreamWrapper.

    Even if interpreted as an variable declaration (it's not), std::istream(input) makes no sense anyway. It would make an istream temporary copy of input's istream subobject, slicing input in the process - except that it wouldn't compile because streams cannot be copied.

    Moreover, since IStreamWrapper stores a reference, it's also not a good idea to use a temporary stream that's going to be immediately destroyed.