Oct 4, 2016

[c++] [cppcon2016] Constant fun

enum  bm { b0 = 0x1, b1 = 0x2, b2 = 0x4 };
int b = bm::b0 | bm::b1;

//---
enum  bm { b0 = 0x1, b1 = 0x2, b2 = 0x4 };
bm b = bm::b0 | bm::b1; // bad conversion

//--
enum  bm { b0 = 0x1, b1 = 0x2, b2 = 0x4 };

bm b = bm(bm::b0 | bm::b1); //OK

//--
enum class bm { b0 = 0x1, b1 = 0x2, b2 = 0x4 };
bm b = bm(bm::b0 | bm::b1); // no such op!!

//--
enum class bm { b0 = 0x1, b1 = 0x2, b2 = 0x4 };

constexpr bm operator| (bm v0, bm v1) {
return bm(int(v0) | int(v1));
}

bm b = bm::b0 | bm::b1;

switch (b) {
case bm::b0 | bm::b1: /*...*/; // OK due to constexpr operator
}

//--


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.