Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

auto

In this response:

https://stackoverflow.com/a/14382318/1676605

this program is given:

std::vector<int> vi{ 0, 2, 4 };
std::vector<std::string> vs{ "1", "3", "5", "7" };
for (auto i : redi::zip(vi, vs))
    std::cout << i.get<0>() << ' ' << i.get<1>() << ' ';

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

In function ‘int main()’:|
/data/cbworkspace/TestZip/TestZip.cpp|14|error: cannot convert ‘boost::iterator_facade<boost::zip_iterator<boost::tuples::tuple<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> > > >, 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 {aka boost::tuples::cons<int&, boost::tuples::cons<int&, boost::tuples::null_type> >}’ to ‘char’ in initialization|
/data/cbworkspace/TestZip/TestZip.cpp|14|warning: unused variable ‘i’ [-Wunused-variable]|
||=== Build finished: 1 errors, 1 warnings (0 minutes, 0 seconds) ===|

Try to figure out the type from that.

Is there a way to figure out what the type a variable of an auto is in C++11? To be more clear, I have a struct like this:

struct EventData
{
    // return value from redi::zip<std::vector<PriceQuote>, std::vector<PriceQuote>> what goes here????? So REDI::Zip is zipping PriceQuote, PriceQuote of bids and asks.
};

struct PriceQuote
{
   double price;
   double size;
};
like image 333
user1676605 Avatar asked Jul 23 '13 18:07

user1676605


People also ask

What is the auto variable type?

In computer programming, an automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or block in which a variable is defined.

How do you determine the type of a variable in CPP?

To get the datatype of variable, use typeid(x). name() of typeinfo library. It returns the type name of the variable as a string.

How do I get auto type in C++?

The following code fragment initializes variable x to type int , variable y to a reference to type const int , and variable fp to a pointer to a function that returns type int . int f(int x) { return x; } int main() { auto x = f(0); const auto& y = f(1); int (*p)(int x); p = f; auto fp = p; //... }

Is Auto a datatype?

The auto keyword can not only be used as the return type for functions but also as the data type of the parameters declared in the function.


1 Answers

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

like image 190
nio Avatar answered Oct 27 '22 08:10

nio