相关疑难解决方法(0)

decltype vs auto

据我了解,双方decltypeauto会尝试找出的东西是什么类型.

如果我们定义:

int foo () {
    return 34;
}
Run Code Online (Sandbox Code Playgroud)

然后这两个声明都是合法的:

auto x = foo();
cout << x << endl;

decltype(foo()) y = 13;
cout << y << endl;
Run Code Online (Sandbox Code Playgroud)

你能否告诉我decltype和之间的主要区别auto是什么?

c++ type-inference

43
推荐指数
2
解决办法
1万
查看次数

什么是decltype以及如何使用?

我无法找到对decltype的一个很好的解释.请告诉我,作为一名初学程序员,它的作用以及它为何有用.

例如,我正在读一本提出以下问题的书.有人可以向我解释答案和原因,以及一些好的(初级)例子吗?

当代码完成时,每个变量的类型和每个变量的值是多少?

int a = 3, b = 4;    
decltype(a) c = a;

decltype((b)) d = a; 

++c; 

++d;
Run Code Online (Sandbox Code Playgroud)

逐行解释将非常有帮助.

c++ decltype c++11

42
推荐指数
1
解决办法
1万
查看次数

我何时应该使用decltype(x)而不是auto来声明变量的类型?

我看到decltype(x)在宏中使用的x是一个变量名,因为在宏内部不知道对象的类型.

例如:

decltype(x) y = expr;
Run Code Online (Sandbox Code Playgroud)

我可以轻松使用auto而不是decltype.那么decltype变量类型声明需要哪些情况而不是auto

c++ c++11

33
推荐指数
6
解决办法
4994
查看次数

标签 统计

c++ ×3

c++11 ×2

decltype ×1

type-inference ×1