Strange error: cannot convert from 'int' to 'ios_base::openmode'

Posted by Dylan Klomparens on Stack Overflow See other posts from Stack Overflow or by Dylan Klomparens
Published on 2010-06-03T19:51:43Z Indexed on 2010/06/03 20:04 UTC
Read the original article Hit count: 259

I am using g++ to compile some code. I wrote the following snippet:

bool WriteAccess = true;
string Name = "my_file.txt";
ofstream File;
ios_base::open_mode Mode = std::ios_base::in | std::ios_base::binary;
if(WriteAccess)
  Mode |= std::ios_base::out | std::ios_base::trunc;
File.open(Name.data(), Mode);

And I receive these errors... any idea why?

Error 1: invalid conversion from ‘int’ to ‘std::_Ios_Openmode’
Error 2: initializing argument 2 of ‘std::basic_filebuf<_CharT, _Traits>* std::basic_filebuf<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]’

As far as I could tell from a Google search, g++ is actually breaking the C++ standard here. Which I find quite astonishing, since they generally conform very strictly to the standard. Is this the case? Or am I doing something wrong.

My reference for the standard: http://www.cplusplus.com/reference/iostream/ofstream/open/

© Stack Overflow or respective owner

Related posts about c++

Related posts about standards-compliance