Topic: My take at a byte literal user-defined operator


Author: =?ISO-8859-1?Q?Pedro_Lamar=E3o?= <pedro.lamarao@gmail.com>
Date: Wed, 25 Nov 2009 14:46:28 CST
Raw View
Is the following well-formed? Valid?
...does it do what I want? :-)



template <unsigned... Bits>
       struct byte_literal;

template <unsigned... Bits>
       unsigned operator "" _byte ()
       {
               return byte_literal<Bits...>::value();
       }

template <unsigned Bit>
       struct byte_literal<Bit>
       {
               static_assert(Bit == 0 || Bit == 1, "a bit literal must
contain only
0s and 1s");
               static const unsigned value = Bit;
       };

template <unsigned Bit, unsigned... Bits>
       struct byte_literal<Bit, Bits...>
       {
               static_assert(sizeof...(Bits) <= 7, "bit literal is too large");
               static const unsigned value = Bit << sizeof...(Bits) |
byte_literal<Bits...>::value;
       };



--
 P.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]