C++11 auto declaration with and without pointer declarator

后端 未结 6 847
借酒劲吻你
借酒劲吻你 2021-02-03 17:08

What\'s the difference between the types of bar1 and bar2?

int foo = 10;
auto bar1 = &foo;
auto *bar2 = &foo;

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 17:42

    Using auto * "documents intention". And auto *p = expr; can be deduced correctly only if expr returns pointer. Example:

    int f();
    
    auto q = f(); // OK
    
    auto *p = f(); // error: unable to deduce 'auto*' from 'f()'
    

提交回复
热议问题