如何实现BOOST_TYPEOF?

时间:2012-08-30 14:32:07

标签: c++ boost types

在这里,我想了解实现BOOST_TYPEOF的一般概念。我的意思是代码可能没问题,但我想代码并不简单,因为它在真正的Boost实现中。因此,我想了解BOOST_TYPEOF实现的想法。它是否使用编译器函数(某些API)来理解编译时表达式的类型?

2 个答案:

答案 0 :(得分:14)

在核心,Boost :: Typeof使用sizeof非计算上下文将表达式的类型转换为整数,然后将其转换回类型。

考虑:

template<int N> struct sizer { char value[N]; };

sizer<1> encode(char);
sizer<2> encode(unsigned char);
sizer<3> encode(signed char);
sizer<4> encode(bool);
...

template<int N> struct decode {};
template<> struct decode<1> { typedef char type; };
template<> struct decode<2> { typedef unsigned char type; };
template<> struct decode<3> { typedef signed char type; };
template<> struct decode<4> { typedef bool type; };

#define TYPEOF(expr) decode<sizeof(encode(expr))>::type

现在,给定一个评估为任何char类型或bool的表达式,我们可以写:

TYPEOF(expr) var = expr;

Boost :: Typeof本质上是这个想法的扩展,最初由Brian Parker于1997年发明;请参阅A Portable "typeof" Operator以了解该想法的讨论和历史。


模板对于这个方案来说有点问题;像std::pair这样简单的东西给出了类型空间的平方,甚至在递归之前。 Boost :: Typeof通过将模板类型及其参数类型编码到编译时链表的连续槽中来解决这个问题:

template<typename List> struct sizer {
    char item0[List::at<0>];
    char item1[List::at<1>];
    char item2[List::at<2>];
    ...
};

template<typename List> struct encode_type<List, char>: append<List, 1> {};
template<typename List> struct encode_type<List, unsigned char>: append<List, 2> {};
template<typename List, typename S, typename T>
struct encode_type<List, std::pair<S, T> >:
    encode_type<encode_type<append<List, 99>, S>, T> {};

template<typename Iter> struct decode_type<1, Iter> {
    typedef char type;
    typedef Iter iter;
};
template<typename Iter> struct decode_type<2, Iter> {
    typedef unsigned char type;
    typedef Iter iter;
};
template<typename Iter> struct decode_type<99, Iter> {
    typedef typename decode_type<Iter::next::value, Iter::next>::type S;
    typedef typename decode_type<Iter::next::value, Iter::next>::iter S_iter;
    typedef typename decode_type<S_Iter::next::value, S_Iter::next>::type T;
    typedef typename decode_type<S_Iter::next::value, S_Iter::next>::iter T_iter;
    typedef std::pair<S, T> type;
    typedef T_iter iter;
};

template<typename List, typename T>
sizer<typename encode_type<List, T>::type> encode(const T&);

template<typename List> struct decode {
    typedef typename decode_type<List::begin::value, List::begin>::type type; };

#define TYPEOF(expr) decode<list<
    sizeof(encode(expr).item0),
    sizeof(encode(expr).item1),
    sizeof(encode(expr).item2),
    ...
    > >::type

这假定现有的编译时链表实现。您会注意到std::pair的解码器消耗列表迭代器中的参数类型所需的项目数量;这本质上是具有非可变类型的语言中等效功能代码的直接翻译。

在标记为省略号...的两行上,我们仅限于类型的固定复杂程度(即构成我们想要推断的类型的模板和类型的数量)。 Boost :: Typeof对于此限制的默认值为50,但是为了提高效率可以降低它,或者为疯狂的复杂程序增加它。

答案 1 :(得分:6)

它基于sizeof是编译时运算符并且可以用作模板参数的想法。这样我们就可以为每种类型分配一个整数,并且可以使用该整数返回该类型。缺点是每种类型都必须手动注册。

E.g:

#include <boost/preprocessor/stringize.hpp>
#include <cstddef>
#include <iostream>

template<size_t> struct TypeId;

#define REGISTER_TYPE(T, id)                                    \
template<> struct TypeId<id> {                                  \
    char value[id];                                             \
    typedef T type;                                             \
    static char const* const name;                              \
};                                                              \
char const* const TypeId<id>::name = BOOST_PP_STRINGIZE(T);     \
TypeId<id> type_to_id(T);

#define TYPEID_(value) TypeId<sizeof(type_to_id(value))>
#define TYPEOF(value) typename TYPEID_(value)::type
#define TYPENAME(value) TYPEID_(value)::name

REGISTER_TYPE(int, 1)
REGISTER_TYPE(unsigned int, 2)
// and so on for all built-in types

int main() {
    int x;
    TYPEOF(x) y;
    std::cout << TYPENAME(y) << '\n';
}

输出:

./test
int