English 中文(简体)
C++11
原标题:how is auto implemented in C++11
  • 时间:2012-04-18 07:51:01
  •  标签:
  • c++
  • c++11

如何使用<条码>auto,载于<条码>C++11? I Trial following and it work in 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);

我想检查如何利用平原<密码>做到这一点。 C++

最佳回答

它在功能模板中用于类型扣减的大致相同,例如:

auto x = 1;

a. 与下列情况相同:

template <class T>
T function(T x) { return input; }

function(1);

汇编者必须列出你作为参数所传递的表述类型,并从中以适当类型的功能模板。 如果我们从此开始,那么<代码>decltype就基本上给我们这个模板中的T,而auto>则给我们这个模板中的“编码>x。

I suspect the similarity to template type deduction made it much easier for the committee to accept auto and decltype into the language -- they basically just add new ways of accessing the type deduction that s already needed for function templates.

问题回答

在C++中,每一表述均 > > > > 和 类型。 例如,<代码>(4+5*2)是一种价值等于14和类型为int的表述。 因此,当你写这封信时:

auto v = (4+5*2);

the compiler detects the type of the expression on the right side, and replaces auto with the detected type (with some exception, read comments), and it becomes:

int v = (4+5*2); //or simply : int v = 14;

Similarly,

auto b = 3.14159; //becomes double b = 3.14159;

auto e = new int[5]; //becomes int* e = new int[5];

页: 1

<代码>auto, 关键词只是一种方式,可以宣布一个变数,同时根据数值加以分类。

页: 1

auto b = 3.14159;

我们知道,b是两倍。

在<代码>auto上添加读物 参考文献如下:

C++ Little Xiaos: The C++11 auto keyword Redux

The C++0x auto keyword

它以前曾做过:

1. 您从未发现过编辑错误,告诉你:

<>strong>error: 无效转换从const char*改为int

缩略语

简而言之,auto <>/code>简单地利用了以下事实:汇编者知道<代码>右侧的表述类型: 签字并重新使用,以打上你宣布的变量。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?