Know When And How To Use auto And decltype In Your Code To Make It Easier To Read And Change And Avoid Mistakes
Have you ever talked with somebody who knew exactly what you meant? I don’t mean how twins can finish each other’s sentences or anything. Simpler things. Like how you don’t know what something is called but after a few descriptive words, the other person nods and motions for you to continue. Even if both of you don’t know the exact name for what you’re talking about, you both know what it is.
Or maybe the other person does know the name. Let’s say that your car is broken and you walk into an auto repair shop. You describe the problem and the mechanic tells you that you need a new timing belt. Or that you need a new water pump.
The mechanic knows exactly what you need. Knows everything about it including all the different variations from one year to the next as the part changed with each new model of your car.
When you’re in this situation and you trust that the other person really does know what you need, do you instead insist that you need something else? What if you get it wrong? And even if you get it right, do you really talk like that? Do you say that you need the water pump version 3.0 for an 83 to 86?
Sure, you can. It can sometimes help eliminate mistakes. If both of you are exact like this then it can help catch the wrong part before it gets delivered and installed.
The problem is that’s a lot to say. If you fully trust the mechanic to know the exact part you need, then you can just refer to it as the broken part and ask that it be installed.
Programming is a lot like this. The compiler knows everything about your program. It has to because it’s responsible for turning your code into a running application. If it doesn’t know what your code means, then it gives you an error.
The compiler acts like the trusted mechanic. If you declare that you want a new int variable and give it the value 1 to start out with, then the compiler already knows you probably want an int because it can see that you’re giving it the value 1. You’ve said enough in your code that the compiler could just nod and give you an int.
An int might not seem like a big deal. But some types are really long and technical. And for other types, only the compiler really ever knows the exact type. For these cases, it helps simplify and make your code easier to understand by telling the compiler to use whatever type it thinks is best.
This is what “auto” does in your code. It lets you declare something where you either don’t know or don’t care to specify what the type is. You give the compiler enough information to figure out the type for itself based on the type of the value you provide to set it up.
This is really useful and actually matches the way we tend to think about things. But there are some things you need to be aware of.
This class will show you how to use the auto type properly in your code as well as how to get declared types with “decltype”.
This is more than just learning the rules. Sometimes there’s no rule that will tell you to use auto or not. This is when having an experienced instructor as a guide will really help you.
Specifically, you’ll learn:
- Why auto and decltype are needed.
- When to use them and when to avoid them.
- How to use auto by itself.
- How to use auto with iterators. Including how to get constant iterators.
- How to use auto with function pointers.
- How to use auto with const and with references.
- How to use decltype but itself.
- And how to use decltype together with auto.