-3

I have some questions regarding when use auto

  1. Explicit, it is not more clear despite the context?
  2. Cases that are better be explicit instead of auto?

And with lambda functions use auto how these

auto f = [](auto v, auto x) {};
//or
std::function<void(auto v, auto x)> f;

Thanks...

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
user3854612
  • 127
  • 1
  • 1
  • 7
  • 1
    The two lines you provided do two very different things. Lambdas are the one case, where you actually HAVE to use auto, because their type is only know to the compiler. [This](http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/) imight be interesting for you. – MikeMB Apr 29 '15 at 20:46
  • 1
    @MikeMB I don't know about that. Neither line compiles, so they sort of do the same thing. One line is CLOSER to compiling? (only missing 2 characters). The other contains fundamental misunderstandings of `auto` as of C++14. – Yakk - Adam Nevraumont Apr 29 '15 at 21:14
  • @Yakk: You are right, that especially the function version doesn't make any sense, whatsoever. But lambdas and std::function are fundamentally different in what they represent and how they are stored. Each lambda is a function object with its own type and is created on the heap. `std::funcion` is wrapper, that can e.g. wrap different functions or function objects, as long as they have the correct interface. Most likely I also misunderstood the question, because I was focuse on when to use auto for variables, not for parameter type deduction for lambdas. – MikeMB Apr 29 '15 at 21:50
  • 1
    @mike lambdas do not use free store/heap storage... not without help. – Yakk - Adam Nevraumont Apr 29 '15 at 23:47
  • @Yakk: Sorry, its too late I meant "stack" of course – MikeMB Apr 29 '15 at 23:52
  • _"Have case that are more better do explict instead of auto?"_ I'm sorry I'm sure it's a language barrier but I have no idea what you're talking about – Lightness Races in Orbit Apr 30 '15 at 02:27

1 Answers1

0

Personally, I have tended to use auto for a select few types of code:

  1. Unutterable types, such as those created by lambdas:

    auto f = []() { /* some code */ };
    
  2. iterators, since they all implement the same opaque interface. I don't care what specific type of iterator it is, just that it acts like one.

    auto it = m.find(key);
    if(it != m.end() {
        /* some code */
    }
    
  3. if the type was already said on the same line, for example casts:

    float f = 123.456;
    /* some code */
    auto n = static_cast<int>(f);
    

    or similarly:

    if(auto p = dynamic_cast<Derived *>(base_ptr)) {
        /* some code here */
    }
    

For everything else, I will tend to be explicit.

Regarding std::function vs auto, I will use auto when I can, and then will fall back on std::function as needed. This is because std::function has a slight overhead since it is implemented using type-erasure techniques.

This usually means that I will use templates to implement functions which accept a "callable thing" when reasonable as well.

That being said, if I need to store a function, possibly pass it around, then std::function is a great fit.

Evan Teran
  • 87,561
  • 32
  • 179
  • 238