139: Data Types: C++ Decltype. Declared Types.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categories:
Sometimes you need to declare a type to be the same as something else. You can use decltype just like auto. When you want to declare a local variable and initialize it right away, then auto really is the better choice. But maybe you want to declare a class member variable and your code won’t get around to initializing it with a value until the constructor or some other member method runs. You can’t use auto in this situation because the compiler doesn’t have anything to initialize the value with. As long as you have the other type available, which you should, then that’s all you need for decltype. This will give you the benefits of declaring a data member type based on some other type that could change as you write your application. And you don’t need to worry about initializing it right away. It will still have a specific type. It also has an added benefit of following the declared type of the expression you pass to decltype exactly. Unlike auto which strips off reference or const qualifiers, the decltype operator will leave those intact. This is actually one case where you might have to use decltype instead of auto even if you are declaring a local method variable and have an initializer handy. I explained in episode 137 how you can force an auto declaration to be const or to be a reference. But what if you don’t want to force it? What if you want your local variable to be a reference if you’re using a reference type to initialize it and you want it to have its own copy if you’re initializing it with a non-reference? The auto declaration by itself will always give you a new copy of the variable. And an auto reference will always give you a reference. If instead, you want something that can more closely follow the actual declared type of the thing being used to initialize your variable, then use decltype. There are other uses of decltype too. You’ll find this operator very useful for all those places where you want some type to follow the type of some expression or other type but aren’t interested in using that type right away as an initializer. Listen to the full episode or you can read the full transcript below. Transcript In a way, this episode builds on episode 137 about auto. I’ll explain the decltype operator today. First of all, this operator should have been called typeof which would have been similar to sizeof and alignof operators. But the typeof operator was never included in the C++ standard so some compiler vendors added their own typeof extension to the language. Then when the C++ standard committee wanted to add typeof to the language, the members were worried it would cause conflicts with code that was already using the unofficial typeof operator. The committee needed a different name for expressing the concept of declared types so they came up with the keyword d e c l t y p e. I’m not sure if it has an agreed on pronunciation, so I call it decltype. There are some differences between decltype and auto. And you can even use them together. But before I start explaining the decltype operator, I want to announce a schedule change for this podcast. I’ve been creating and producing a new episode every weekday for the last eight months but there’s some other projects that need my time. Beginning with this episode, the podcast schedule will change to twice a week on Mondays and Fridays. The topics are becoming more advanced and can sometimes take four hours or more for me to research and prepare a well thought out episode. That’s not a schedule I can maintain and it really struck me as I was preparing this episode. I’ve been reading all the details and digging into as many dark corners of C++ as I could to present what will be less than ten minutes. This episode alone has taken half a day. Okay, maybe I did get sidetracked a few times. Don’t get me wrong. I love doing this. But I’m working on some really cool