3

I am starting picking up c++11 and at this point I have a love-n-hate of "auto".

No doubt it is convenient.

But it is also confusing, here is a code:

auto w = foo->get_w();
auto i = w->get_i();
bar(i);

In this code, get_w() function returns "struct S*" which is w's type. But struct S has 2 overloaded get_i():

const I* get_i() const;
unique_ptr<I>& get_i();

Because w is not const, so it is the 2nd get_i() gets called, and thus bar(i) should be bar(move(i)) because bar() signature is void bar(unique_ptr<I>).

But in reality, it is easy to get track lost.

It is good that this is caught in compile time, but, i guess, my initial enthusiasm is waning.

How do you think? Any tip?

my_question
  • 3,075
  • 2
  • 27
  • 44
  • What is the prototype of `bar`? – E_net4 Sep 04 '14 at 16:46
  • 7
    My opinion is that the problem here is with confusing const-overloading. The two types are completely unrelated. You may want to talk with the author of the struct `S`. – milleniumbug Sep 04 '14 at 16:47
  • 2
    Seems rather odd that you would overload a function with completely different signatures. –  Sep 04 '14 at 16:47
  • I think your question may be too subjective for this site, as it asks for opinions ("What do you think?"). – milleniumbug Sep 04 '14 at 17:05
  • 1
    I think it's primarily opinion based. Some find `auto` amazing, other hate it, and there are all the nuances in between. Pick your nuance :) I do agree that sometimes it gets confusing, but on the other hand, I use auto most of the time for my iterators. A rule of thumb I keep is that it should not be confusing. Then, the definitions of "confusing" may vary... – JBL Sep 04 '14 at 17:11
  • 2
    Also, `auto i = w->get_i();` won't compile if `get_i()` is returning a `unique_ptr&`. – T.C. Sep 04 '14 at 17:12
  • 2
    The problem with your code isn't the use of `auto` but the terrible choice of names like `foo`, `get_w`, `get_i` and `bar`. – fredoverflow Sep 04 '14 at 17:17

2 Answers2

4

For this question, I have to link this excellent article by Herb Sutter: GotW #94 Solution: AAA Style (Almost Always Auto).

You can read his opinion already from the title. He also brings up this guideline the rationale of which is explained in the article:

Guideline: Remember that preferring auto variables is motivated primarily by correctness, performance, maintainability, and robustness—and only lastly about typing convenience.

The main aspects about auto that he praises is that it avoids unwanted implicit conversions, of which there are plenty in C++ and that it makes callers less dependent on functions interfaces, since a change in return type is automatically reflected in the local variables' type. auto is also very useful in template code, since you don't have to access e.g. Container::value_type.

Oberon
  • 3,219
  • 15
  • 30
2

It depends. Whenever it's more convenient than confusing, then use it, else don't. For a long list of arguments and opinions I'll refer to another, different question: Use of var keyword in C#

Community
  • 1
  • 1
Peter
  • 5,608
  • 1
  • 24
  • 43