在c ++ 11中的指针的'auto'类型赋值是否需要'*'? [英] Does 'auto' type assignments of a pointer in c++11 require '*'?
问题描述
给定我的变量是一个指针,如果我将它分配给一个auto类型的变量,是否指定*?
std :: vector< MyClass> * getVector(); //返回填充的向量
// ...
std :: vector< MyClass> * myvector = getVector(); //假设有n个项目
auto newvar1 = myvector;
// vs:
auto * newvar2 = myvector;
//目标是表现得像这样的赋值:
std :: vector< MyClass> * newvar3 = getVector();
我对这个 auto有点困惑
在c ++ 11中工作(这是c ++ 11的新功能,对吗?)
更新:以上更好地阐明我的矢量是如何真正填充在一个函数,我只是试图将返回的指针分配给一个变量。对不起,
auto newvar1 = myvector;
// vs:
auto * newvar2 = myvector;
这两个都是一样的,并且会声明一个指向 std ::向量< MyClass>
(指向随机位置,因为。所以基本上你可以使用任何一个。我喜欢 myvector
在您的示例中未初始化,可能包含垃圾) auto var = getVector()
,但你可以去 auto * var = getVector()
认为它强调的意图( var
是一个指针)更好。
梦想类似的不确定性使用 auto
。我认为人们只是使用 auto
,而不是想到它,这是正确的99%的时间 - 需要装饰自动
只有参考和cv限定符。
但是,是稍微修改:
auto newvar1 = myvector,newvar2 = something;
在这种情况下, newvar2
auto * newvar1 = myvector,newvar2 = something;
这里, newvar2
是pointee类型,例如。 std :: vector< MyClass>
,初始值必须足够。
不是支持的初始化列表,编译器会像这样处理 auto
:
-
它产生一个人工函数模板声明,其中一个参数是声明的确切形式,
auto
由模板参数替换。因此,对于auto * x = ...
,它使用< class T> void foo(T *);
-
它尝试解析调用
foo(initializer) code>,并且查看推导出
T
的内容。 -
如果单个声明中有更多的声明器,这是为他们所有。
Given my variable being a pointer, if I assign it to a variable of "auto" type, do I specify the "*" ?
std::vector<MyClass> *getVector(); //returns populated vector //... std::vector<MyClass> *myvector = getVector(); //assume has n items in it auto newvar1 = myvector; // vs: auto *newvar2 = myvector; //goal is to behave like this assignment: std::vector<MyClass> *newvar3 = getVector();
I'm a bit confused on how this
auto
works in c++11 (this is a new feature to c++11, right?)Update: I revised the above to better clarify how my vector is really populated in a function, and I'm just trying to assign the returned pointer to a variable. Sorry for the confusion
解决方案auto newvar1 = myvector; // vs: auto *newvar2 = myvector;
Both of these are the same and will declare a pointer to
std::vector<MyClass>
(pointing to random location, since. So basically you can use any one of them. I would prefermyvector
is uninitialized in your example and likely contains garbage)auto var = getVector()
, but you may go forauto* var = getVector()
if you think it stresses the intent (thatvar
is a pointer) better.I must say I never dreamt of similar uncertainity using
auto
. I thought people would just useauto
and not think about it, which is correct 99 % of the time - the need to decorateauto
with something only comes with references and cv-qualifiers.However, there is slight difference between the two when modifies slightly:
auto newvar1 = myvector, newvar2 = something;
In this case,
newvar2
will be a pointer (and something must be too).auto *newvar1 = myvector, newvar2 = something;
Here,
newvar2
is the pointee type, eg.std::vector<MyClass>
, and the initializer must be adequate.In general, if the initializer is not a braced initializer list, the compiler processes
auto
like this:It produces an artificial function template declaration with one argument of the exact form of the declarator, with
auto
replaced by the template parameter. So forauto* x = ...
, it usestemplate <class T> void foo(T*);
It tries to resolve the call
foo(initializer)
, and looks what gets deduced forT
. This gets substituted back in place ofauto
.If there are more declarators in a single declarations, this is done for all of them. The deduced
T
must be the same for all of them...
这篇关于在c ++ 11中的指针的'auto'类型赋值是否需要'*'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!