1 // Copyright (C) 2007 Peder Holt
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
7 # define BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED
9 # include <boost/config.hpp>
10 # include <boost/detail/workaround.hpp>
11 # include <boost/mpl/int.hpp>
18 template<int N> struct encode_counter : encode_counter<N - 1> {};
19 template<> struct encode_counter<0> {};
21 char (*encode_index(...))[1];
23 # define BOOST_TYPEOF_INDEX(T) (sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1000>*)0)))
24 # define BOOST_TYPEOF_NEXT_INDEX(next) friend char (*encode_index(encode_counter<next>*))[next];
30 struct msvc_extract_type
35 template<typename T, typename ID>
36 struct msvc_register_type : msvc_extract_type<ID>
38 typedef msvc_extract_type<ID> base_type;
39 struct base_type::id2type // This uses nice VC6.5 and VC7.1 bugfeature, also works for Digital Mars
47 struct msvc_typeid_wrapper {
48 typedef typename msvc_extract_type<mpl::int_<ID> >::id2type id2type;
49 typedef typename id2type::type type;
56 //Get the next available compile time constants index
57 BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T));
58 //Instantiate the template
59 typedef typename msvc_register_type<T,mpl::int_<value> >::id2type type;
60 //Set the next compile time constants index
61 BOOST_STATIC_CONSTANT(unsigned,next=value+1);
62 //Increment the compile time constant (only needed when extensions are not active
63 BOOST_TYPEOF_NEXT_INDEX(next);
69 typedef char(*type)[encode_type<T>::value];
73 typename sizer<T>::type encode_start(T const&);
75 template<typename Organizer, typename T>
76 msvc_register_type<T,Organizer> typeof_register_type(const T&,Organizer* =0);
78 # define BOOST_TYPEOF(expr) \
79 boost::type_of::msvc_typeid_wrapper<sizeof(*boost::type_of::encode_start(expr))>::type
81 # define BOOST_TYPEOF_TPL(expr) typename BOOST_TYPEOF(expr)
83 # define BOOST_TYPEOF_NESTED_TYPEDEF_TPL(name,expr) \
85 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
86 typedef typename boost::type_of::msvc_extract_type<name>::id2type id2type;\
87 typedef typename id2type::type type;\
90 # define BOOST_TYPEOF_NESTED_TYPEDEF(name,expr) \
92 BOOST_STATIC_CONSTANT(int,_typeof_register_value=sizeof(boost::type_of::typeof_register_type<name>(expr)));\
93 typedef boost::type_of::msvc_extract_type<name>::id2type id2type;\
94 typedef id2type::type type;\
100 #endif//BOOST_TYPEOF_MSVC_TYPEOF_IMPL_HPP_INCLUDED