在c ++ 11中的指针的'auto'类型赋值是否需要'*'? [英] Does 'auto' type assignments of a pointer in c++11 require '*'?

查看:778
本文介绍了在c ++ 11中的指针的'auto'类型赋值是否需要'*'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定我的变量是一个指针,如果我将它分配给一个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


  1. 它产生一个人工函数模板声明,其中一个参数是声明的确切形式, auto 由模板参数替换。因此,对于 auto * x = ... ,它使用

      < class T> void foo(T *); 


  2. 它尝试解析调用 foo(initializer) code>,并且查看推导出 T 的内容。


  3. 如果单个声明中有更多的声明器,这是为他们所有。

    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 myvector is 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 var is a pointer) better.

    I must say I never dreamt of similar uncertainity using 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.

    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:

    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. 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 T must be the same for all of them...

    这篇关于在c ++ 11中的指针的'auto'类型赋值是否需要'*'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆