I've spent some hours debugging an access violation. It turned out that a std::ifstream was sliced to a std::istream due to a typo in the code. I'm curious whether it is legal what happened to me, because the compiler first calls ifstream(ifstream&&) and then istream(istream&&), although the object is only returned once. Please see the code I provided.#include <cassert>#include <fstream>std::ifstream OpenRead(const char* file) { std::ifstream stream(file, std::ios_base::in | std::ios_base::binary); if( !stream.good() ) throw "Unable to open file!"; stream.exceptions(std::ios_base::badbit); return stream;}int main() { static const char* filename = "main.cpp"; std::istream input = OpenRead(filename); // access violation //std::ifstream input = OpenRead(filename); // fixes the bug char test = input.get(); assert( test == '#' );}
Visual Studio/Silverlight/Tooling version
What category (if any) best represents this feedback?
Steps to reproduce
Product Language
Operating System
Operating System Language
Actual results
Expected results
Please wait...