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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12773257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:36:31  来源:igfitidea点击:

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

c++pointersc++11auto

提问by Dolan Antenucci

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 autoworks 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

更新:我修改了上面的内容以更好地阐明我的向量是如何真正填充到函数中的,我只是试图将返回的指针分配给一个变量。对困惑感到抱歉

采纳答案by jpalecek

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 myvectoris uninitialized in your example and likely contains garbage). 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 varis a pointer) better.

这两个是相同的,并将声明一个指向 std::vector<MyClass>(指向随机位置,因为myvector在您的示例中未初始化并且可能包含垃圾). 所以基本上你可以使用其中的任何一种。我更喜欢auto var = getVector(),但auto* var = getVector()如果您认为它var更好地强调意图(即指针),您可以选择。

I must say I never dreamt of similar uncertainity using auto. I thought people would just use autoand not think about it, which is correct 99 % of the time - the need to decorate autowith something only comes with references and cv-qualifiers.

我必须说我从未梦想过使用auto. 我认为人们只会使用auto而不去考虑它,这在 99% 的情况下是正确的 - 需要auto用引用和 cv 限定符来装饰。

However, there isslight difference between the two when modifies slightly:

然而,就是这两个修改时稍微之间细微的差别:

auto newvar1 = myvector, newvar2 = something;

In this case, newvar2will be a pointer (and something must be too).

在这种情况下,newvar2将是一个指针(并且某些东西也必须是)。

auto *newvar1 = myvector, newvar2 = something;

Here, newvar2is the pointee type, eg. std::vector<MyClass>, and the initializer must be adequate.

这里,newvar2是指针类型,例如。std::vector<MyClass>,并且初始化程序必须足够。

In general, if the initializer is not a braced initializer list, the compiler processes autolike this:

一般来说,如果初始化器不是花括号初始化器列表,编译器处理auto如下:

  1. It produces an artificial function template declaration with one argument of the exact form of the declarator, with autoreplaced by the template parameter. So for auto* x = ..., it uses

    template <class T> void foo(T*);
    
  2. It tries to resolve the call foo(initializer), and looks what gets deduced for T. This gets substituted back in place of auto.

  3. If there are more declarators in a single declarations, this is done for all of them. The deduced Tmust be the same for all of them...

  1. 它生成一个人工函数模板声明,其中一个参数与声明符的确切形式相同,auto并被模板参数替换。所以对于auto* x = ...,它使用

    template <class T> void foo(T*);
    
  2. 它尝试解析调用foo(initializer),并查看推断的内容T。这被替换回代替auto.

  3. 如果单个声明中有更多声明符,则对所有声明符都这样做。推导出的T对他们所有人来说必须是一样的......

回答by Pibben

There is a, perhaps subtle, difference between autoand auto*when it comes to constness.

有一个,也许是微妙的,之间的差异auto,并auto*当谈到常量性。

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
}

回答by Stacker

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;or to create another pointer to the same vector use auto newvar1 = myvector;. The difference to your other attempt auto *newvar1 = myvector;is that the latter once forces myvector to be of pointer type, so the following code fails:

这可能就是您想要的,它创建了实际向量的副本。如果您想要引用而不是写入auto& newvar1 = *myvector;或创建另一个指向同一向量的指针,请使用auto newvar1 = myvector;. 与您的其他尝试的不同之auto *newvar1 = myvector;处在于,后者曾经强制 myvector 为指针类型,因此以下代码失败:

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