简体   繁体   English

c ++ 11中指针的“自动”类型分配是否需要“*”?

[英]Does 'auto' type assignments of a pointer in c++11 require '*'?

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?)我对这个auto在 c++11 中的工作方式有点困惑(这是 c++11 的一个新特性,对吧?)

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>这两个是相同的,并且会声明一个指向std::vector<MyClass>的指针(pointing to random location, since myvector is uninitialized in your example and likely contains garbage) (指向随机位置,因为myvector在您的示例中未初始化并且可能包含垃圾) . . So basically you can use any one of them.所以基本上你可以使用其中的任何一种。 I would prefer auto var = getVector() , but you may go for auto* var = getVector() if you think it stresses the intent (that var is a pointer) better.我更喜欢auto var = getVector() ,但如果你认为它更好地强调意图( var是一个指针),你可以选择auto* var = getVector()

I must say I never dreamt of similar uncertainity using auto .我必须说我从来没有想过使用auto遇到类似的不确定性。 I thought people would just use auto and not think about it, which is correct 99 % of the time - the need to decorate auto with something only comes with references and cv-qualifiers.我认为人们只会使用auto而不会考虑它,这在 99% 的情况下是正确的 - 需要用引用和 cv 限定符来装饰auto

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).在这种情况下, newvar2将是一个指针(并且某些东西也必须是)。

auto *newvar1 = myvector, newvar2 = something;

Here, newvar2 is the pointee type, eg.在这里, newvar2是指针类型,例如。 std::vector<MyClass> , and the initializer must be adequate. std::vector<MyClass> ,并且初始值设定项必须足够。

In general, if the initializer is not a braced initializer list, the compiler processes auto like this:一般来说,如果初始化器不是花括号初始化器列表,编译器会像这样处理auto

  1. It produces an artificial function template declaration with one argument of the exact form of the declarator, with auto replaced by the template parameter.它生成一个人工函数模板声明,其中一个参数与声明符的形式完全相同, auto替换为模板参数。 So for auto* x = ... , it uses所以对于auto* x = ... ,它使用

    template <class T> void foo(T*);
  2. It tries to resolve the call foo(initializer) , and looks what gets deduced for T .它尝试解析调用foo(initializer) ,并查看为T推导出的内容。 This gets substituted back in place of auto .这被替换回auto

  3. 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...所有这些推导出的T必须相同......

There is a, perhaps subtle, difference between auto and auto* when it comes to constness.就常量而言, autoauto*之间可能存在细微的差异。

int i;
const auto* p = &i;

is equivalent to相当于

int i;
const int* p = &i;

whereas然而

int i;
const auto p = &i;

is equivalent to相当于

int i;
int* const p = &i;

This has the following effect:这具有以下效果:

void test(int a) {
    const auto* p1 = &a;

    *p1 = 7;               // Error
    p1 = nullptr;          // OK

    const auto p2 = &a;

    *p2 = 7;               // OK
    p2 = nullptr;          // Error
}
auto newvar1 = *myvector;

This is probably what you want, which creates a copy of the actual vector.这可能就是您想要的,它创建了实际向量的副本。 If you want to have a reference instead write auto& newvar1 = *myvector;如果你想有一个参考而不是写auto& newvar1 = *myvector; or to create another pointer to the same vector use auto newvar1 = myvector;或者创建另一个指向同一向量的指针,使用auto newvar1 = myvector; . . The difference to your other attempt auto *newvar1 = myvector;与您其他尝试的区别auto *newvar1 = myvector; is that the latter once forces myvector to be of pointer type, so the following code fails:是后者曾经强制 myvector 为指针类型,因此以下代码失败:

std::vector<int> v1;
auto* v2 = v1; // error: unable to deduce ‘auto*’ from ‘v1’

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM