C++11 auto declaration with and without pointer declarator

C++C++11AutoVariable Declaration

C++ Problem Overview


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

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

If both bar1 and bar2 are int*, does it makes sense to write the pointer declarator (*) in the bar2 declaration?

C++ Solutions


Solution 1 - C++

The declarations are exactly equivalent. auto works (almost) the same as template type deduction. Putting the star explicitly makes the code a bit easier to read, and makes the programmer aware that bar2 is a pointer.

Solution 2 - C++

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()'

Solution 3 - C++

There is a big difference when you use const qualifiers:

int i;

// Const pointer to non-const int
const auto ip1 = &i; // int *const
++ip1; // error
*ip1 = 1; // OK

// Non-const pointer to const int
const auto* ip2 = &i; // int const*
++ip2; // OK
*ip2 = 1; // error

Solution 4 - C++

In this specific example both bar1 and bar2 are the same. It's a matter of personal preference though I'd say that bar2 is easier to read.

However, this does not hold true for references as seen in this example:

#include <iostream>
using namespace std;
 
int main() {
	int k = 10;
	int& foo = k;
	auto bar = foo; //value of foo is copied and loses reference qualifier!
	bar = 5; //foo / k won't be 5
	cout << "bar : " << bar << " foo : " << foo << " k : " << k << endl;
	auto& ref = foo;
	ref = 5; // foo / k will be 5
	cout << "bar : " << bar << " foo : " << foo << " k : " << k;
	return 0;
}

Solution 5 - C++

As others said, they'll generate the same code. The asterisk is line noise (and makes it harder to switch from raw pointers to smart pointers if, for example, &foo is ever replaced by get_foo()). If you want to be explicit, then by all means, be explicit; but when you're using type inference, just let the compiler do its job. Lack of asterisks does not imply that an object isn't a pointer.

Solution 6 - C++

It doesn't matter as far as the interpretation of the C++ code goes; you can write whatever you want. However, there is a question of style and readability: Generally, you should not hide pointer, reference and CV qualifiers, and perhaps even smart pointers, in type aliases, since it makes it harder for the reader to understand that that's what's going on. Type aliases should package the semantically relevant type content, whereas qualifiers and modifiers should remain visible. So prefer the following:

 using Foo = long_namespace::Foobrigation<other_namespace::Thing>;
 using MyFn = const X * (int, int);

 std::unique_ptr<Foo> MakeThatThing(MyFn & fn, int x)   // or "MyFn * fn"
 { 
     const auto * p = fn(x, -x);
     return p ? p->Create() : nullptr;
 }

And don't say:

using PFoo = std::unique_ptr<Foo>;   // just spell it out
using MyFn = int(&)(int, int);       // unnecessary; & is easy to spell
auto p = fn(x, -x);                  // Don't know that p is a pointer

Note also that reference qualifiers (unlike pointers) genuinely change the type of the variable that's being declared, so they're not optional:

X & f();
auto a = f();    // copy!
auto & b = f();  // b is the same as the return value of f()

Finally, adding explicit const pointer qualifications can help const-correctness. Consider the next example, in which a container contains pointers-to-mutable, but we only require const access. Just auto * would deduce a pointer to mutable, which we can avoid by saying const explicitly:

std::vector<X*> v = /* ... */;
for (const auto * p : v)
{
    observe(p->foo());   // no need for a mutable *p
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHenry BarkerView Question on Stackoverflow
Solution 1 - C++vsoftcoView Answer on Stackoverflow
Solution 2 - C++Victor DyachenkoView Answer on Stackoverflow
Solution 3 - C++danplaView Answer on Stackoverflow
Solution 4 - C++Hatted RoosterView Answer on Stackoverflow
Solution 5 - C++Jeff SchwabView Answer on Stackoverflow
Solution 6 - C++Kerrek SBView Answer on Stackoverflow