1

Edit2: Guys, sorry but I find it annoying that an answer is marked as duplicate without actually checking if it solves one's problem. The suggested SO answer does not fit the stated problem. It is fixed number of variables vs. variable number of variables + variable combination of ranges of integers and integer values. If you still think the right direction, then please provide a valid solution to my stated problem or just don't mark it simply as duplicate if you can't do so.

I'm trying to find the most elegant way to define a bitmask. This bitmask is an integer, and it defines the visibility of objects on a map (32 levels, so bits 0..31 define visibility on each of the 32 levels).

What I ideall would like to have:

int visibilityMask = CreateVisibilityMask(0..12, 16, 22);

So the above would be a variadic function that creates the bit mask, where the .. needs to be overloaded to create a bitmask by itself which would then be OR'ed with the other values.

I guess this one is really tough. But is it impossible?

Edit: Sorry guys, but if you think this answer is a duplicate of the answer you suggested, then please create an example of how the other one could be used according to my needs. The provides answer doesn't allow any combination of a range, and single int values in a variable - if you think it is easy to resolve, please provide.

Note: The original question was about overloading "..", which is not possible. Thanks for clarification on this.

benjist
  • 2,740
  • 3
  • 31
  • 58
  • 5
    No. `..` is not an operator. – Sani Huttunen May 03 '13 at 12:11
  • 2
    The asnwer is basically the same as for [the colon operator](http://stackoverflow.com/questions/16339207/implementing-matlabs-colon-operator-in-c-expression-templates-class/16339594#16339594) - in fact, the *question* is basically the same as well. – Arne Mertz May 03 '13 at 12:17
  • How about a `class range { public: range(int a, int b); }`? – Micha Wiedenmann May 03 '13 at 12:51
  • @SaniHuttunen On the other hand `.` and `...` are, but you still can't do the requested thing as both operands are primitives. Also, `.` would be parsed as part of a float rather than the operator in this case. – dascandy May 03 '13 at 12:51
  • 1
    @dascandy: No, neither `.` nor `...` is an _operator_. The set of C++ operators is limited, and exhaustively listed in the Standard. So this question really should be phrased as "is it possible to **add** `operator..` ?". Overloading implies there's an existing version. – MSalters May 03 '13 at 13:13

2 Answers2

1

No, but you can do something like this:

#define bitrange(from,to)  (unsigned(-1) >> from << from << (sizeof(int)-to) >> (sizeof(int)-to)) 

int visibilityMask = bitrange(0,12)  |  1<<16  |  1<<22;

ANSWER TO COMMENT THREAD (this is too long for comments).

I write exclusively in C+11 now. Mostly it is complex metaprograming libraries.

I would used constexpr function myself in this case. This is despite the fact that I've filed several consexpr bugs with GCC and CLANG (gcc-54648,clang-13970).

This was quick and dirty code showing idea how to do this. And I've wrote "something like this". It is trivial rewrite this with constexpr for me, but I won't. I've wrote this for OP, which probably does not have C++11 complier and might not know what constexpr is. OP was not asking about style guide, he needed solution. If you guys have better solution, please post your answer.

Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
  • If you want to do this in C++ I must advise using a constexpr function instead. Functionally the same but at least it's not an ugly define that transcends all hierarchy. Also, bitshifting negative numbers is UB and probably not something you want to rely on; especially for such a simple task. – dascandy May 03 '13 at 12:50
  • `constexpr` is C++11, OP was asking about C++, not about C++11. For negatives, you are correct. I've - added `unsigned`. – Leonid Volnitsky May 03 '13 at 12:59
  • 2
    I believe we should assume C++11 unless specified differently. – qdii May 03 '13 at 13:03
  • All C++ compilers assume by default pre C++11 code. To enable C++11 you need to add special switch. On SO you need to add `C++11` tag if question is about C++11 – Leonid Volnitsky May 03 '13 at 13:06
  • Agree with qdii. ISO has withdrawn C++03. – MSalters May 03 '13 at 13:07
  • @LeonidVolnitsky: What do compiler defaults have to do with anything, even if your assertion that they all default to a historic version of the language were true? My C compiler defaults to C90; should we assume that all C questions are asking about a previous century's version of the language? – Mike Seymour May 03 '13 at 13:20
  • Also, we have the [C++03] tag for questions about the previous C++ standard. I.e. you can't deduce from the existance of the [C++11] tag that the [C++] tag means [C++03]. – MSalters May 03 '13 at 13:28
  • Thanks, your answer helped me to find a solution, though not exactly the one I was hoping for. Your define though didn't work, the bitmask was incorrect. – benjist May 03 '13 at 18:27
0

I'm trying to find the most elegant way to define a bitmask.

Consider std::bitset instead?

codah
  • 466
  • 4
  • 14