I have two containers:
std::vector< ObjectClass > vecD; // container of objects
std::vector< ObjectClass* > vecP; // container of pointers
in my code, I want to loop over all elements. As far as I know, I need to write distinct for loops, that means
// container of objects
for ( const auto& elem : vecD )
elem.doStuff();
// container of pointers
for ( const auto& elem : vecP )
elem->doStuff(); // the "->" is needed instead of "."
Is there a way to tell the loop "if the elemets are objects, use them directly. Otherwise, dereference them first"?
update Here is a more elaborate example to clarify:
I have those containers. These are each used in a templated function:
template< typename ContainerT >
void myfunc( const ContainerT& container)
{
for ( const auto& elem : container )
{
if ( elem_is_a_pointer ) //how can this work?
elem->doStuff(); // member function
else
elem.doStuff();
}
}