Iterating over TArray with for

The lecture presents the following code as preventing changes to Actor:

for (const auto& Actor : OverlappingActors)
{
    // Use Actor
}

This isn’t the case, since auto is deduced to be AActor* and const will apply to the outermost type (that is deduced by auto), or the pointer in this case. This means that the type of Actor is Actor* const& and Visual Studio will show this in a tooltip when hovering over Actor. The corresponding code to make Actor const, would be:

for (const auto* Actor : OverlappingActors)
{
    // Use Actor
}

In this case, Actor is deduced to be AActor and the const applies to AActor, just as it would if you’d typed AActor instead of auto.

There’s a great talk by Scott Meyers about type deduction here.

17 Likes

That’s very interesting thanks for sharing that link. I will let @ben know about that.

@ahmedcharles thanks again for this, this video is slated to be patched soon. We just need to resolve an issue with its captions first.

Much appreciated.

2 Likes

@ahmedcharles just to let you know I’m finally uploading a patch to this video now, thanks again.

2 Likes

Cool. Thanks.

Are you saying that auto is inferred according to whether it is * or & after const?

How does

const AActor* &

turn into

AActor* const& ?

anyone know?

I think your problem is that you are trying to point to a reference, basically this is impossible, so what is happening is VS is assuming you are trying to declare an AActor* called “const&”, so if you get rid of the & or the *, it should work, assuming this is what you are asking? BTW, i’m rather new to this syntax, so I might be completely misunderstanding what you are saying.

The talk by Scott Meyers looks like it’s great and well explained, but there are too many things I don’t know about C++ that seem to be prerequisites for that talk.
I have no idea about templates or L-type-references, so I guess I’ll ignore the whole thing for now and bookmark it for later.

But thanks for providing background infos!

One questions though:
The original code from Ben doesn’t crash or behave unexpectedly, so the only thing that is wrong with it is about the const declaration, right?

1 Like

If auto is supposed to just give to you the type you need, why you still need to specify if it is a pointer or a ref?

I am just using const auto, and it works properly and picks a pointer:
for (const auto Actor : OverlappingActors)

Thanks :wink:

That works.

The reason you might want to use auto with a const, * or & is to change the type. So, you can do auto& r = s; and if s is a std::string, then r will be std::string&.

If you want to change the type use a cast, don’t you? :slight_smile:

I mean, one of the advantages of auto is performance, because you ensure there is not an implicit type conversion (wasting performance) going on.

I can understand the use of &, that will bring you the variable by reference instead of a copy.

Using auto with *… I really don’t get it.

Good to know, and the youtube linked somehow helped, it’s not my strongest point and I might have to do more to understand it.

sorry for the late reply but thanks I will probably give it a try. but I am currently waiting for the revision in the Unreal course so I can start over fresh and hopefully focus more on the course and apply what I learned to make my projects better and hopefully more efficient :blush:

Privacy & Terms