Question

If you're looping through a container as such:

typedef std::vector<std::unique_ptr<BaseClass>> Container;
Container container;

for(Container::const_iterator element = container.begin(); element != container.end(); element++)
{
    //Read through values
}

And instead of using the typedef you decide to use auto:

std::vector<std::unique_ptr<BaseClass>> container;

for(auto element = container.begin(); element != container.end(); element++)
{
    //Read through values
}

Assuming you don't alter the values, does the auto keyword use a const iterator over a non const one?

This question is curiosity more than anything, the only reason I can see this being an applicable question in a real life scenario would be if you needed to communicate that you weren't to alter values to another person working on the code.

Was it helpful?

Solution

1) Use cbegin and cend to be explicit about using const iterator.

2) begin() and end() return const_iterator when method is declared as const

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top