
On 29 May 2010 00:05, Terry Golubiewski <tjgolubi@netins.net> wrote:
What is Magic<int, int>?
Something that needs implementing as a test of an endian library. For example, I could implement it with what I wrote as follows: template <int In, int Out> struct Magic { array< uint_t<In>::least, Out/In > in; unsigned i; Magic() : i() {} void put(uint_t<In>::least x) { in[i++] = x; } uint_t<Out>::least get() const { array<uint_t<Out>::least, 1> out; pack<big_bit, In, Out>(in, out); return out[1]; } }; With Beman's it would be something like this: template <int In, int Out> struct Magic { union { array< endian<big, uint_t<In>::exact>, Out/In> in; endian<big, uint_t<Out>::exact> out; }; array< uint_t<In>::least, Out/In > in; unsigned i; Magic() : i() {} void put(uint_t<In>::least x) { in[i++] = x; } uint_t<Out>::least get() const { return out; } }; I think, anyways, though it requires C++0x unions. That, of course, can't handle Ins or Outs that are not multiples of CHAR_BIT.