Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

c++ - Absence of typeof operator in C++03?

I'm just wondering how boost have implemented BOOST_TYPEOF (in C++03) which seems to be a very useful tool. Anyone has any idea?

Also, I'm thinking C++03 itself could have provided typeof operator, especially when it already has sizeof(expr) which must be knowing the type of the expr also, otherwise how else could it tell us the size, without knowing the type?Is it really possible to know the size, without knowing the type of an expression?

If it doesn't know the type, then compiler tells us the size of what (if not type)? I mean, sizeof(unknowntype) wouldn't make sense to the compilers (and humans as well)!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It just uses compiler magics. Like, GCC's __typeof__. For compilers that don't provide such magic, it provides an emulation that can detect the type of some expressions, but fails with completely unknown types.

A possible implementation could be to have a list of functions that accept an expression of a given type, and then dispatch from that type to a number using a class template. To make the function template return the number as a compile time entity, we put it into an array dimension

template<typename> struct type2num;
template<int> struct num2type;
template<typename T> typename type2num<T>::dim &dispatch(T const&);

Then it goes from that number back to the type, so that our EMUL_TYPEOF could directly name the type. So to register a type, we write

#define REGISTER_TYPE(T, N) 
  template<> 
  struct type2num<T> { 
    static int const value = N; 
    typedef char dim[N]; 
  }; 
  template<> 
  struct num2type<N> { typedef T type; }

Having this in place, you can write

#define EMUL_TYPEOF(E) 
  num2type<sizeof dispatch(E)>::type

Whenever you need to register a type, you write

REGISTER_TYPE(int, 1);
REGISTER_TYPE(unsigned int, 2);
// ...

Of course, now you find you need a mechanism to accept vector<T>, where you don't know T in advance and then it gets arbitrary complex. You could create a system where the numbers mean more than just a type. This could probably work:

#define EMUL_TYPEOF(E) 
  build_type<sizeof dispatch_1(E), sizeof dispatch_2(E)>::type

This could detect types like int and also types like shared_ptr<int> - in other words, types that aren't class template specializations, and class template specializations with one template argument, by doing some kind of systematical mapping

  • If the first number yields 1, the second number specifies a type; otherwise
  • the first number specifies a template, and the second number its first type template argument

So this becomes

template<int N, int M>
struct build_type {
  typedef typename num2tmp<N>::template apply<
    typename num2type<M>::type>::type type;
};

template<int N>
struct build_type<1, N> {
  typedef num2type<N>::type type;
};

We also need to change the dispatch template and split it up in two versions, shown below, alongside the REGISTER_TEMP1 for registering one-argument templates

template<typename T> typename type2num<T>::dim1 &dispatch_1(T const&);
template<typename T> typename type2num<T>::dim2 &dispatch_2(T const&);

#define REGISTER_TYPE(T, N) 
  template<> 
  struct type2num<T> { 
    static int const value_dim1 = 1; 
    static int const value_dim2 = N; 
    typedef char dim1[value_dim1]; 
    typedef char dim2[value_dim2]; 
  }; 
  template<> 
  struct num2type<N> { typedef T type; }

#define REGISTER_TMP1(Te, N) 
  template<typename T1> 
  struct type2num< Te<T1> > { 
    static int const value_dim1 = N; 
    static int const value_dim2 = type2num<T1>::value_dim2; 
    typedef char dim1[value_dim1]; 
    typedef char dim2[value_dim2]; 
  }; 
  template<> struct num2tmp<N> { 
    template<typename T1> struct apply { 
      typedef Te<T1> type; 
    }; 
  }

Registering the std::vector template and both int variants now look like

REGISTER_TMP1(std::vector, 2);
// ... REGISTER_TMP1(std::list, 3);

REGISTER_TYPE(int, 1);
REGISTER_TYPE(unsigned int, 2);
// ... REGISTER_TYPE(char, 3);

You probably also want to register multiple numbers with each type, one number for each combination of const/volatile or may need more than one number per type for recording *, & and such. You also want to support vector< vector<int> >, so you need more than one number for the template argument too, making build_type call itself recursively. As you can create an arbitrary long list of integers, you can encode anything into that sequence anyway, so it's just up to your creativity on how to represent these things.

In the end, you are probably reimplementing BOOST_TYPEOF :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...