mercredi 30 août 2017

Create typedef only when typedef exists in template parameter

I'm trying to create universal container wrapper.

template<typename type>
class ContainerWrapper
{
public:
  using allocator_type = typename type::allocator_type;
  using size_type = typename type::size_type;
  using difference_type = typename type::difference_type;
  using pointer = typename type::pointer;
  using const_pointer = typename type::const_pointer;
  using reference = typename type::reference;
  using const_reference = typename type::const_reference;
  using iterator = typename type::iterator;
  using const_iterator = typename type::const_iterator;
  using reverse_iterator = typename type::reverse_iterator;
  using const_reverse_iterator = typename type::const_reverse_iterator;
  using value_type = typename type::value_type;

  iterator begin() noexcept { return container.begin(); }
  const_iterator begin() const noexcept { return container.begin(); }
  iterator end() noexcept { return container.end(); }
  const_iterator end() const noexcept { return container.end(); }
  reverse_iterator rbegin() noexcept { return container.rbegin(); }
  const_reverse_iterator rbegin() const noexcept { return container.rbegin(); }
  reverse_iterator rend() noexcept { return container.rend(); }
  const_reverse_iterator rend() const noexcept { return container.rend(); }
  const_iterator cbegin() const noexcept { return container.cbegin(); }
  const_iterator cend() const noexcept { return container.cend(); }
  const_reverse_iterator crbegin() const noexcept { return container.crbegin(); }
  const_reverse_iterator crend() const noexcept { return container.crend(); }
protected:
  ContainerWrapper() {}
  type& getContainer() { return container; }
  const type& getContainer() const { return container; }
private:
  type container;
};

But not all containers have all kinds of iterators. Is it possible to expose container types only when they exists? Something like

using const_reverse_iterator = typename std::enable_if_t<std::is_class<typename type::const_reverse_iterator>::value, typename type::const_reverse_iterator>::type;

I could use C++11 only (gcc 4.7). Of course I could create different wrappers for different containers, but I prefer to have one universal wrapper.

Aucun commentaire:

Enregistrer un commentaire