7

I had a typo (|| instead of |) and noticed such a code fails with GCC and compiles with Visual. I know that the second parameter of std::ifstream is an int. So theoretically, a bool has to be converted implicitely to an int. So why it fails?

Example inducing the error (I just used some ints instead of the flags).

#include <fstream>

int main(int argc, char * argv[]) {
  std::ifstream("foo", 2 | 3 || 4)
}
YSC
  • 38,212
  • 9
  • 96
  • 149
dgrat
  • 2,214
  • 4
  • 24
  • 46

1 Answers1

9

std::ifstream's constructor takes as second argument an std::ios_base::openmode which is typedefed from an implementation defined type:

typedef /*implementation defined*/ openmode;

It seems Visual uses integers, GCC does not, and it's why your code fails on GCC.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • Thx for the answer. Regardless of the reason, it seems a bit odd to me. What could be the reason that the type of a standard library function parameter is implementation defined. Couldn't it cause a portability issue? – dgrat Nov 23 '17 at 13:29
  • @dgrat you're only supposed to use the `openmode` explicitly defined. – YSC Nov 23 '17 at 13:30
  • I wonder a bit about all the up-votes of my question. Expected the opposite, because seems like a beginner issue. – dgrat Nov 23 '17 at 14:25
  • @dgrat when life gives you lemons... I up-voted your question because I didn't know why your code worked with MSVC but not GCC, and it was fun to learn why. – YSC Nov 23 '17 at 14:27