Problem with enum and binary or operator

Started by
51 comments, last by ChaosEngine 6 years, 9 months ago
10 minutes ago, alvaro said:

It's not just allowed: It's completely idiomatic. Take for instance std::ios_base::openmode . I just looked at its implementation in gcc 7.1.0 and it's an enum, where a few constants have been defined as powers of 2 and you then use them like this:



  std::ofstream ofs;
  ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);

 

If you are not familiar with this idiom, you just don't know C++.

[EDIT: By the way, in gcc 7.1.0 <bits/ios_base.h> defines the operators &, |, ^, ~, |=, &= and ^= for this type.]

According to cppreference, the exact type of std::ios_base::openmode is implementation defined.

http://en.cppreference.com/w/cpp/io/ios_base/openmode

Again, though, just because one particular implementation of the standard library does it, doesn't mean it's actually a good idea.

Advertisement
6 hours ago, Oberon_Command said:

According to cppreference, the exact type of std::ios_base::openmode is implementation defined.

http://en.cppreference.com/w/cpp/io/ios_base/openmode

Again, though, just because one particular implementation of the standard library does it, doesn't mean it's actually a good idea.

Yep, Visual Studio defines openmode as an int.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement