Ara*_*raK 6 c++ boost if-statement metaprogramming c++11
我只是在学习C++中的元编程基础知识,我觉得很高兴看到其他人如何解决以下问题.另外,看到使用Boost元编程库的解决方案会非常好,因为我认为它们对我来说是一个黑暗的角落.所以问题是,这可以更优雅地重写吗?
假设我们有以下结构:
template <std::size_t size>
struct type_factory
{
typedef typename type_factory_impl<size>::type type;
};
Run Code Online (Sandbox Code Playgroud)
这个结构应该是typedef type,取决于的值size.type_factory_impl是实施type_factory.用于确定的算法type是:
if(size % bits<unsigned long long>::value == 0)
typedef unsigned long long type;
else if(size % bits<unsigned long>::value == 0)
typedef unsigned long type;
else if(size % bits<unsigned int>::value == 0)
typedef unsigned int type;
else if(size % bits<unsigned short int>::value == 0)
typedef unsigned short int type;
else if(size % bits<unsigned char>::value == 0)
typedef unsigned char type;
else
static_assert(false, "The type should be multiple of 'unsigned char' size");
Run Code Online (Sandbox Code Playgroud)
我用两种方式解决了这个元程序.第一个是直接使用模式匹配,第二个使用meta if-else.请考虑以下两种解决方案之间的通用代码:
#include <cstddef>
#include <climits>
typedef unsigned char uchar;
typedef unsigned short int usint;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ulonglong;
// Returns how many bits in Unsigned_Type
template <typename Unsigned_Type>
struct bits
{ enum { value = sizeof(Unsigned_Type)*CHAR_BIT }; };
// struct type_factory_impl ...
template <std::size_t size>
struct type_factory
{
typedef typename type_factory_impl<size>::type type;
};
int main()
{
auto a = type_factory<8>::type(0); // unsigned char
auto b = type_factory<16>::type(0); // unsigned short int
auto c = type_factory<24>::type(0); // unsigned char
auto d = type_factory<32>::type(0); // unsigned long
auto e = type_factory<40>::type(0); // unsigned char
auto f = type_factory<48>::type(0); // unsigned short int
auto g = type_factory<56>::type(0); // unsigned char
auto h = type_factory<64>::type(0); // unsigned long long
}
Run Code Online (Sandbox Code Playgroud)
template <bool is_uchar>
struct unsigned_char
{
typedef unsigned char type;
static_assert(is_uchar,
"error: size must be multiple of 'unsigned char' size");
};
template <>
struct unsigned_char <true>
{ typedef uchar type; };
template <bool is_usint, std::size_t size>
struct unsigned_short_int
{ typedef typename
unsigned_char<size % bits<uchar>::value == 0>::type type; };
template <std::size_t size>
struct unsigned_short_int <true, size>
{ typedef usint type; };
template <bool is_uint, std::size_t size>
struct unsigned_int
{ typedef typename
unsigned_short_int<size % bits<usint>::value == 0, size>::type type; };
template <std::size_t size>
struct unsigned_int <true, size>
{ typedef uint type; };
template <bool is_ulong, std::size_t size>
struct unsigned_long
{ typedef typename
unsigned_int<size % bits<uint>::value == 0, size>::type type; };
template <std::size_t size>
struct unsigned_long <true, size>
{ typedef ulong type; };
template <bool is_ulonglong, std::size_t size>
struct unsigned_long_long
{ typedef typename
unsigned_long<size % bits<ulong>::value == 0, size>::type type; };
template <std::size_t size>
struct unsigned_long_long <true, size>
{ typedef ulonglong type; };
template <std::size_t size>
struct type_factory_impl
{ typedef typename
unsigned_long_long<size % bits<ulonglong>::value == 0, size>::type type; };
Run Code Online (Sandbox Code Playgroud)
template <bool condition, typename Then, typename Else>
struct IF
{ typedef Else type; };
template <typename Then, typename Else>
struct IF <true, Then, Else>
{ typedef Then type; };
template <std::size_t size>
struct type_factory_impl
{
typedef typename
IF<size % bits<ulonglong>::value == 0, ulonglong,
typename IF<size % bits<ulong>::value == 0, ulong,
typename IF<size % bits<uint>::value == 0, uint,
typename IF<size % bits<usint>::value == 0, usint,
typename IF<size % bits<uchar>::value == 0, uchar, uchar>::type
>::type
>::type
>::type
>::type type;
};
Run Code Online (Sandbox Code Playgroud)
和您一样,我认为 Boost.MPL 是黑魔法,所以我认为这可能是尝试使用它来回答您的问题的机会。请记住,这是我第一次尝试使用这个库,并且那里的一些专家可能会提供更好的解决方案。
这个想法是使用boost::mpl::find_if来查找类型序列中的第一个匹配项。
typedef boost::mpl::vector
<
unsigned long long,
unsigned long,
unsigned int,
unsigned short,
unsigned char
> type_sequence;
template<std::size_t size>
struct predicate
{
template<class T>
struct apply {
static const bool value = (size % bits<T>::value == 0);
};
};
template<std::size_t size>
struct type_factory_impl
{
typedef typename boost::mpl::find_if
<
type_sequence,
typename predicate<size>::apply<boost::mpl::_1>
>::type iterator_type;
typedef typename boost::mpl::deref<iterator_type>::type type;
};
Run Code Online (Sandbox Code Playgroud)
它似乎给了我很好的结果:

我不处理“默认”情况,但我的大脑刚刚开始从鼻子流血,我稍后会尝试完成我的答案,希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
2760 次 |
| 最近记录: |