error : expected primary-expression before 'decltype'
error : expected primary-expression before 'decltype'
我正在尝试查找变量的类型。在 Whosebug 中提到 decltype()
用于该目的。但是当我尝试使用它时,它抛出了我在标题中提到的错误。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 4;
cout << decltype(x);
return 0;
}
我预计 int
但它显示为错误。 error: expected primary-expression before 'decltype'
类型不是第一个 class 对象。您不能将类型传递给函数,而 cout << decltype(x)
正是这样,将类型传递给函数(尽管被运算符美化了)。
要获取有关变量类型的信息,您可以
- 阅读代码。如果对象的类型是
int
,请不要打印它。
- 使用调试器单步执行程序。它显示变量的类型。
使用这个(非标准)函数模板
template <class T> void printType(const T&)
{
std::cout << __PRETTY_FUNCTION__ << "\n";
}
printType(x);
使用增强功能。
#include <boost/type_index.hpp>
std::cout << boost::typeindex::type_id_with_cvr<decltype(x)>().pretty_name() << "\n";
我正在尝试查找变量的类型。在 Whosebug 中提到 decltype()
用于该目的。但是当我尝试使用它时,它抛出了我在标题中提到的错误。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 4;
cout << decltype(x);
return 0;
}
我预计 int
但它显示为错误。 error: expected primary-expression before 'decltype'
类型不是第一个 class 对象。您不能将类型传递给函数,而 cout << decltype(x)
正是这样,将类型传递给函数(尽管被运算符美化了)。
要获取有关变量类型的信息,您可以
- 阅读代码。如果对象的类型是
int
,请不要打印它。 - 使用调试器单步执行程序。它显示变量的类型。
使用这个(非标准)函数模板
template <class T> void printType(const T&) { std::cout << __PRETTY_FUNCTION__ << "\n"; } printType(x);
使用增强功能。
#include <boost/type_index.hpp> std::cout << boost::typeindex::type_id_with_cvr<decltype(x)>().pretty_name() << "\n";