boost的integer/integer_mask.hpp仅仅做了单个位的bit mask
要多个位必须写很多遍high_bit_mask_t
使用low_bits_mask_t也不能完全解决问题
所以自己用Typelist的那种写法写了一个
用法举例
bit_mask<INT_LIST_2(2, 3)>::value返回一个值,该值的第2、3位被置为1
其余位为0
namespace
?multi_bit_mask

{
????
namespace
?details

????
{

????????
namespace
?storage_definition

????????
{
????????????template?
<
int
?Bit
>
????????????
struct
?bit_storage

????????????
{
????????????????typedef?typename?bit_storage
<
Bit?
-
?
1
>
::storage_type?storage_type;
????????????}
;

????????????
//
~~~~~~~~~~~~~~~platform?dependency,?modify?it?when?things?go?wrong~~~~~~~~~~~~~~~~~~~~~~~~~~
????????????typedef?unsigned?
int
?smallest_storage_type;

????????????template?
<>
????????????
struct
?bit_storage
<
0
>
????????????
{
????????????????typedef?smallest_storage_type?storage_type;
????????????}
;

????????????template?
<>
????????????
struct
?bit_storage
<
32
>
????????????
{
????????????????typedef?unsigned?
long
?
long
?storage_type;
????????????}
;

????????????
//
disable?the?65th?bit
????????????template?
<>
????????????
struct
?bit_storage
<
64
>
????????????
{
????????????????typedef?
void
?storage_type;
????????????}
;

????????????

????????????
//
~~~~~~~~~~~~~~~~~~~~~~~~~~end?of?platform?dependency~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
????????}
????????
namespace
?int_list_definition

????????
{
????????????template?
<
unsigned?
int
?N,?typename?Next
>
????????????
struct
?int_list

????????????
{
????????????????typedef?typename?storage_definition::bit_storage
<
N
>
::storage_type?storage_type;
????????????????
static
?
const
?storage_type?value?
=
?N;
????????????????typedef?Next?next;
????????????}
;


????????????
struct
?null_type
{}
;

????????????

????????????template
<
typename?T1,?typename?T2,?
bool
?is_first
>
????????????
struct
?selector

????????????
{
????????????????typedef?T1?type;
????????????}
;

????????????template
<
typename?T1,?typename?T2
>
????????????
struct
?compare_type

????????????
{
????????????????
const
?
static
?
bool
?is_larger?
=
?
sizeof
(T1)?
>
?
sizeof
(T2);
????????????????typedef?typename?selector
<
T1,?T2,?is_larger
>
::type?large_type;
????????????????typedef?typename?selector
<
T1,?T2,?
!
is_larger
>
::type?small_type;
????????????}
;

????????????

????????????template
<
typename?T1,?typename?T2
>
????????????
struct
?selector
<
T1,?T2,?
false
>
????????????
{
????????????????typedef?T2?type;
????????????}
;

????????????template?
<
typename?List,?
int
?N
>
????????????
struct
?delete_from

????????????
{
????????????
private
:
????????????????typedef?typename?delete_from
<
typename?List::next,?N
>
::result_type?deleted_next;
????????????
public
:
????????????????typedef?int_list
<
List::value,?deleted_next
>
?result_type;
????????????}
;

????????????template?
<
int
?N
>
????????????
struct
?delete_from
<
null_type,?N
>
????????????
{
????????????????typedef?null_type?result_type;
????????????}
;

????????????template?
<
typename?Next,?
int
?N
>
????????????
struct
?delete_from
<
int_list
<
N,?Next
>
,?N
>
????????????
{
????????????????typedef?typename?delete_from
<
Next,?N
>
::result_type?result_type;
????????????}
;


????????????template?
<
typename?List
>
????????????
struct
?make_unique

????????????
{
????????????
private
:
????????????????typedef?typename?make_unique
<
typename?List::next
>
::result_type?uniqued_next;
????????????????typedef?typename?delete_from
<
uniqued_next,?List::value
>
::result_type?prepared_next;
????????????
public
:
????????????????typedef?int_list
<
List::value,?prepared_next
>
?result_type;
????????????}
;

????????????template?
<>
????????????
struct
?make_unique
<
null_type
>
????????????
{
????????????????typedef?null_type?result_type;
????????????}
;
????????}
????????template?
<
typename?List
>
????????
class
?find_largest_storage

????????
{
????????????typedef?typename?find_largest_storage
<
typename?List::next
>
::storage_type?T1;
????????????typedef?typename?storage_definition::bit_storage
<
List::value
>
::storage_type?T2;
????????
public
:
????????????typedef?typename?int_list_definition::compare_type
<
T1,?T2
>
::large_type?storage_type;
????????}
;

????????template?
<>
????????
class
?find_largest_storage
<
int_list_definition::null_type
>
????????
{
????????
public
:
????????????typedef?storage_definition::smallest_storage_type?storage_type;
????????}
;
????}
????template?
<
int
?N
>
????
struct
?single_bit_mask

????
{
????????typedef?typename?details::storage_definition::bit_storage
<
N
>
::storage_type?storage_type;
????????
static
?
const
?storage_type?value?
????????????
=
?static_cast
<
storage_type
>
(single_bit_mask
<
N?
-
?
1
>
::value)?
*
?
2
;
????}
;

????template?
<>
????
struct
?single_bit_mask
<
0
>
????
{
????????typedef?details::storage_definition::bit_storage
<
0
>
::storage_type?storage_type;
????????
static
?
const
?storage_type?value?
=
?
1
;
????}
;

????


????template?
<
typename?List
>
????
struct
?bit_mask

????
{
????
private
:
????????typedef?typename?details::int_list_definition::make_unique
<
List
>
::result_type?List_;
????
public
:

????????typedef?typename?details::find_largest_storage
<
List_
>
::storage_type?storage_type;
????
????????
static
?
const
?storage_type?value?
????????????
=
?static_cast
<
storage_type
>
(single_bit_mask
<
List_::value
>
::value)?
????????????
+
?static_cast
<
storage_type
>
(bit_mask
<
typename?List_::next
>
::value);
????}
;

????template?
<>
????
struct
?bit_mask
<
details::int_list_definition::null_type
>
????
{
????????typedef?details::storage_definition::bit_storage
<
0
>
::storage_type?storage_type;
????????
static
?
const
?storage_type?value?
=
?
0
;
????}
;


????typedef?multi_bit_mask::details::int_list_definition::null_type?null_type;

????template?
<
int
?N,?typename?Next
>
????
struct
?int_list_t?:?
public
?multi_bit_mask::details::int_list_definition::int_list
<
N,?Next
>
?
{}
;

????
#define
?INT_LIST_1(n1)?multi_bit_mask::int_list_t<n1,?multi_bit_mask::null_type>
????
#define
?INT_LIST_2(n1,?n2)?multi_bit_mask::int_list_t<n1,?INT_LIST_1(n2)?>?
????
#define
?INT_LIST_3(n1,?n2,?n3)?multi_bit_mask::int_list_t<n1,?INT_LIST_2(n2,?n3)?>?
????
#define
?INT_LIST_4(n1,?n2,?n3,?n4)?multi_bit_mask::int_list_t<n1,?INT_LIST_3(n2,?n3,?n4)?>?
????
#define
?INT_LIST_5(n1,?n2,?n3,?n4,?n5)?multi_bit_mask::int_list_t<n1,?INT_LIST_4(n2,?n3,?n4,?n5)?>?
????
#define
?INT_LIST_6(n1,?n2,?n3,?n4,?n5,?n6)?multi_bit_mask::int_list_t<n1,?INT_LIST_5(n2,?n3,?n4,?n5,?n6)?>?
????
#define
?INT_LIST_7(n1,?n2,?n3,?n4,?n5,?n6,?n7)?multi_bit_mask::int_list_t<n1,?INT_LIST_6(n2,?n3,?n4,?n5,?n6,?n7)?>?
????
#define
?INT_LIST_8(n1,?n2,?n3,?n4,?n5,?n6,?n7,?n8)?multi_bit_mask::int_list_t<n1,?INT_LIST_7(n2,?n3,?n4,?n5,?n6,?n7,?n8)?>?
????
}
sample
#include?
<
iostream
>
#include?
"
multi_bit_mask.h
"
using
?
namespace
?std;
int
?main()

{
????cout?
<<
?multi_bit_mask::bit_mask
<
INT_LIST_1(
1
)
>
::value?
<<
?endl;
????cout?
<<
?multi_bit_mask::bit_mask
<
INT_LIST_5(
0
,?
1
,?
2
,?
3
,?
4
)
>
::value?
<<
?endl;
????cout?
<<
?multi_bit_mask::bit_mask
<
INT_LIST_7(
0
,?
1
,?
2
,?
3
,?
4
,?
4
,?
2
)
>
::value?
<<
?endl;
????