Menu ▾ ▴

#21 compiler warning: std::streamoff is not size_t

v1.0 (example)
open
nobody
None
5
2014-07-02
2014-05-07
Constantin
No

On Windows 7 with Visual Studio 2012 the following code:

void openFile(const std::string& path){
rapidxml::xml_document<> doc;
rapidxml::file<> xmlFile(path.c_str());
doc.parse<0>(xmlFile.data());
}

will result in the compiler warning:

rapidxml_utils.hpp(40): warning C4244: 'initializing' : conversion from 'std::streamoff' to 'size_t', possible loss of data
1> rapidxml_utils.hpp(28) : while compiling class template member function 'rapidxml::file<>::file(const char *)'
1> header.h(33) : see reference to function template instantiation 'rapidxml::file<>::file(const char *)' being compiled
1> header.h(33) : see reference to class template instantiation 'rapidxml::file<>' being compiled

Because size_t is used in the class rapidxml::file, but tellg() returns a std::streampos / std::fpos. Explicit casting to size_t solves the problem for me.

Discussion

  • Weber

    Weber - 2014-07-02

    Casting away the warning is not enough here, because proper error handling for tellg() returning -1 is still missing.

     

Log in to post a comment.