Posts

Showing posts with the label templates

Why list iterator has three template arguments in SGI STL's implemention?

Why list iterator has three template arguments in SGI STL's implemention? I have a problem when I'm reading SGI STL's implementation of list iterator. template<class T> struct __list_node { void *prev; void *next; T data; }; template<class T, class Ref, class Ptr> struct __list_iterator { typedef __list_iterator<T, T&, T*> iterator; typedef __list_iterator<T, Ref, Ptr> self; ... typedef T value_type; typedef Ptr pointer; typedef Ref reference; typedef __list_node<T>* link_type; ... link_type node; ... reference operator*() const { return (*node).data; } pointer operator-> const { return &(operator*()); } self& operator++() { ... } self operator++(int) { ... } ... ... }; So, why are there three template arguments? What if only class T exists? Something likes below. class T template<class T> struct __list_iterator { typedef __list_iterator<T...

Ambiguous resolution with template conversion operator

Ambiguous resolution with template conversion operator I had to do a similar code: #include <type_traits> template<typename S> struct probe { template<typename T, typename U = S, std::enable_if_t< std::is_same<T&, U>::value && !std::is_const<T>::value, int> = 0> operator T& () const; template<typename T, typename U = S&&, std::enable_if_t< std::is_same<T&&, U>::value && !std::is_const<T>::value, int> = 0> operator T&& (); template<typename T, typename U = S, std::enable_if_t< std::is_same<T const&, U>::value, int> = 0> operator T const& () const; template<typename T, typename U = S&&, std::enable_if_t< std::is_same<T const&&, U>::value, int> = 0> operator T const&& () const; }; struct some_type {}; struct other_type {}; auto test_call...