L
LR
blargg said:Excuse my preference for the preprocessor
I think this might be a good place for the preprocessor, although the
usual caveats probably apply. I approached this from a slightly
different perspective.
I wonder if something like this might satisfy the OP:
#include <iostream>
#include <limits>
#include <bitset>
static const unsigned int DigitsInAnUnsignedLong =
std::numeric_limits<unsigned long>::digits;
typedef std::bitset<DigitsInAnUnsignedLong> BinaryType;
// this uses an explicit ctor
#define Binary(Z) BinaryType(std::string(#Z)).to_ulong()
int main() {
std::cout << Binary(11) << std::endl;
}
I don't particularly think that 'Binary' is a good name for a define,
but that's something to be careful about anyway, and this is just an
example, more a point of departure than a solution.
There might be some portability issues, and issues if you need signed
types, but maybe that can be taken care of with some abstraction.
LR