Search code examples
c++c++11syntaxcoding-styleauto

Any difference between `auto* p = &n` and `auto p = &n`?


#include <type_traits>

int main()
{
    int n;

    auto* p1 = &n;
    auto  p2 = &n;

    static_assert(std::is_same_v<decltype(p1), decltype(p2)>); // ok
}

Any difference between auto* p = &n and auto p = &n? or just a coding style issue?


Solution

  • Any difference between auto* p = &n and auto p = &n?

    No difference.

    or just a coding style issue?

    Yes, for some it may be clearer with auto* p1 = &n; that it's a pointer.

    Note, however, the cpp core guideline seems to favour later/simpler style auto p2 = &n; perhaps because it's consistent with the usage of smart pointer - which doesn't need any * to start with, example:

    auto p = make_shared<X>(2);