Здравствуйте, Аноним, Вы писали:
А>Есть примерно вот такой код (общая идея):
А>Как это можно сделать наиболее "легким" способом? А может не стоит огород городить и оставить как есть? Хотелось бы найти наиболее правильное решение. Спасибо.
по моему, так не стоит усложнять, если имеющееся решение Вас устраивает.
по приведенной же ситуации можно сделать вот такие варианты —
enum Type{ one, two, three, four, five };
struct null_type{};
template <typename THead, typename TTail>
struct type_list_node
{
typedef THead head_type;
typedef TTail tail_type;
};
template < Type t >
struct enum_to_type
{
static const Type value = t;
};
template <Type t>
struct Test
{
static bool In( Type forTest )
{
return t == forTest;
}
};
template <Type t1, Type t2>
struct Test2
{
static bool In( Type forTest )
{
return Test<t1>::In(forTest) || Test<t2>::In(forTest);
}
};
template <Type t1, Type t2, Type t3>
struct Test3
{
static bool In( Type forTest )
{
return Test2<t1, t2>::In(forTest) || Test<t3>::In(forTest);
}
};
template <Type t>
struct enum_list1 : type_list_node< enum_to_type<t>, null_type > {};
template <Type t1, Type t2>
struct enum_list2 : type_list_node< enum_to_type<t1>, enum_list1<t2> > {};
template <Type t1, Type t2, Type t3>
struct enum_list3 : type_list_node< enum_to_type<t1>, enum_list2<t2, t3> > {};
template < typename TList >
class tester
{
typedef typename TList::head_type head_type;
typedef typename TList::tail_type tail_type;
template < typename head, typename tail >
struct test_impl
{
static bool in( Type t )
{
return (t == head::value) || tester< tail >::in(t);
}
};
template < typename head >
struct test_impl<head, null_type>
{
static bool in( Type t )
{
return (t == head::value);
}
};
public :
static bool in( Type t )
{
return test_impl< head_type, tail_type >::in(t);
}
};
void test()
{
/* первый вариант */
bool b =
Test<one>::In(one)
&& Test2<one, two>::In(two)
&& Test3<one, two, three>::In(three);
/* второй вариант */
b = tester< enum_list1<one> >::in( one );
b = tester< enum_list2<one, two> >::in( one );
b = tester< enum_list3<one, two, three> >::in( four );
return;
}
насколько такие 'огороды' подходят для задачи, решайте сами.