据我了解,双方decltype并auto会尝试找出的东西是什么类型.
如果我们定义:
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是什么?
我无法找到对decltype的一个很好的解释.请告诉我,作为一名初学程序员,它的作用以及它为何有用.
例如,我正在读一本提出以下问题的书.有人可以向我解释答案和原因,以及一些好的(初级)例子吗?
当代码完成时,每个变量的类型和每个变量的值是多少?
Run Code Online (Sandbox Code Playgroud)int a = 3, b = 4; decltype(a) c = a; decltype((b)) d = a; ++c; ++d;
逐行解释将非常有帮助.
我看到decltype(x)在宏中使用的x是一个变量名,因为在宏内部不知道对象的类型.
例如:
decltype(x) y = expr;
Run Code Online (Sandbox Code Playgroud)
我可以轻松使用auto而不是decltype.那么decltype变量类型声明需要哪些情况而不是auto?
如何auto实施C++11?我试过跟随,它的工作原理C++11
auto a = 1;
// auto = double
auto b = 3.14159;
// auto = vector<wstring>::iterator
vector<wstring> myStrings;
auto c = myStrings.begin();
// auto = vector<vector<char> >::iterator
vector<vector<char> > myCharacterGrid;
auto d = myCharacterGrid.begin();
// auto = (int *)
auto e = new int[5];
// auto = double
auto f = floor(b);
Run Code Online (Sandbox Code Playgroud)
我想检查一下如何使用plain来实现 C++
由于auto和decltype都用于推断类型.我以为他们会一样的.
但是,这个问题的答案表明不然.
我仍然认为他们不可能完全不同.我可以想到一个简单的例子,其中i两种情况下的类型相同.
auto i = 10; and decltype(10) i = 10;
Run Code Online (Sandbox Code Playgroud)
那么auto和decltype行为相同的可能情况是什么呢?