I am trying to find the type of variable. In stackoverflow it is mentioned that decltype() is used for that purpose. But when I tried to used it is throwing me the error as I mentioned in title.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 4;
cout << decltype(x);
return 0;
}
I expected int but it showing as error. error: expected primary-expression before 'decltype'
Types aren't first class objects. You can't pass a type to a function, and cout << decltype(x) is exactly that, passing a type to a function (though beautified by the operator).
To get an info about the type of a variable, you can
int, don't bother printing it.Use this (non-standard) function template
template <class T> void printType(const T&)
{
std::cout << __PRETTY_FUNCTION__ << "\n";
}
printType(x);
Use Boost.
#include <boost/type_index.hpp>
std::cout << boost::typeindex::type_id_with_cvr<decltype(x)>().pretty_name() << "\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With