How can I determine the actual type of an 'auto' variable

Why do you want to put that type in a struct? It's not really designed to be used like that (I should know, I wrote it!) but if necessary you can use decltype and std::declvalto determine the type (which will still give the right answer if I change the implementation of redi::zip)

struct EventData
{
  // type returned by redi::zip
  typedef decltype(redi::zip(std::declval<V1>(), std::declval<V2>())) zipper_type;

  // type referred to by zipper_type::iterator
  typedef std::iterator_traits<zipper_type::iterator>::value_type zipped_type;

  zipper_type m_zipper;
};

N.B. why are you creating a typedef for the struct? This is C++ not C, stop it.

I have no idea what the type of auto i is, making it harder to reuse expertise and learn from examples.

Get used to it. Do you know the type that std::bind returns? Do you know the type that std::mem_fn returns? Do you know the type that a lambda expression creates? No, you don't need to know, all you need to know is what properties it has and what you can do with it, not what it's called or what types it contains.


An answer for

"How can I determine the actual type of an 'auto' variable at compile time"

Answer:

Try compiling something like this:

auto foo = function_that_returns_unknown_type() // "what type could foo be?"
int a = foo;

The compiler error message will tell you that "type XXX (whatever that is) cannot be converted to int". There you have your type


Try to change auto into a char and read the error message.


Would you have found

for (boost::iterator_facade<
       boost::zip_iterator<
         boost::tuples::tuple<std::vector<int>::iterator,
                              std::vector<int>::iterator>
       >,
       boost::tuples::cons<int&, boost::tuples::cons<int&, boost::tuples::null_type> >,
       boost::random_access_traversal_tag,
       boost::tuples::cons<int&, boost::tuples::cons<int&, boost::tuples::null_type> >,
       long int
     >::reference i : redi::zip(vi, vs))
    std::cout << i.get<0>() << ' ' << i.get<1>() << ' ';

easier to understand?

Tags:

C++

C++11

Auto