0

Without inheriting from vector, how do I get all of its typedefs?

template <typename T>
class MyStack
{
public:
    typename std::vector<T>::reference top()
    {
        return storage.front();
    }
private:
    std::vector<T> storage;
};

std::vector<T>::reference is too much typing. I'm looking for something like iterator category except for containers instead of iterators. But I heard you're not allowed to inherit from standard containers.

  • About my answer, I think the problem arises because the compiler cannot prove that every `std::vector` contains the nested type `reference` (e.g., there could be a separate translation unit that specializes `std::vector` to not have one and this translation unit cannot possibly have knowledge of that when compiled in isolation). I'm not sure what governs the difference between using `reference` and `typename container_type_aliases>::reference` that causes the latter to work. Something involving dependent vs. nondependent name lookup. – chris Mar 06 '15 at 18:05
  • Found it: http://stackoverflow.com/questions/1643035/propagating-typedef-from-based-to-derived-class-for-template. So, my technique will work in general, but not for classes that derive from a use of it that depends on a template parameter. (Also, I should have said qualified vs. unqualified name lookup) – chris Mar 06 '15 at 18:07

1 Answers1

0

Use typedef to make it shorter

typedef std::vector<T>::reference brief_t;

EDITED You can inherit from std:: container but it is not recomended for reasons from https://stackoverflow.com/a/7110262/149818. Just to make record shorter pick one of the ways:

  • as my one above;
  • make typedef std::vector<T> super_t and use super_t::reference;
  • if you need just pick specific result of member use decltype: typedef typename decltype( std::vector<T>().size() ) mysize_t;
Community
  • 1
  • 1
Dewfy
  • 23,277
  • 13
  • 73
  • 121