I hope I understood what you are asking correctly.
Generally, if your code asks you to perform type casts or type comparisions, it indicates a design problem.
You can solve this by using generic code, that makes as few assumptions about the code as possible. To do this, use the generic container adapters in std (std::begin, std::end) and generic algorithms (see std::find, std::accumulate, std::for_each, std::transform).
You can also typedef
your data type and write everything in terms of the typedef (and related types).
Example:
class A{
typedef std::list<Course*> courseptr_sequence; // consider
// either shared_ptr or unique_ptr instead of raw*
courseptr_sequence courses;
public:
// an advanced course is - for this example
// a course where Course::advanced() evaluates to true
void find_advanced_courses(std::ostream& out) const
{
auto advanced = std::find_if(std::begin(courses), std::end(courses),
[](Course* p){ return p->advanced(); });
if(advanced != std::end(courses))
out << *(*advanced);
}
};
With this kind of implementation, you can change the container type in the typedef and the rest will compile alright.
Edit: If your question was about two different container types where you compare elements at corresponding positions (and the elements have different types), the answer is to use std::equal with a custom comparison functor.
Edit (see reference1 and reference2):
template <class N>
struct is_list { static const int value = 0; };
template <class N, class A>
struct is_list<std::list<N, A> > { static const int value = 1; };
template <typename T> using negate = std::integral_constant<bool, !T::value>;
template<typename C>
void user3630497_sort(std::enable_if<
negate<is_list<C>::value>::value, C>::type& container)
{
std::sort(std::begin(container), std::end(container));
}
template<typename C>
void user3630497_sort(std::enable_if<
is_list<C>::value, C>::type& container)
{
container.sort();
}
usage:
std::list<Course*> l;
user3630497_sort(l);
std::vector<Course*> v;
user3630497_sort(l);