mercredi 28 mars 2018

Template subclass of template class as parameter to fully specialized function

I built a custom templated iterator as a subclass of a templated container:

template< typename T, class U, typename V >
class aContainer {
  public:
    template< bool ABool, bool BBool >
    class aIterator {
      public:
        using iterator_category = std::forward_iterator_tag;
        using value_type = T;
        using difference_type = std::ptrdiff_t;
        using referenc = T&;
        using pointer = T*;
       /** iterator related operations */
    };
  protected:
    T const * const _begin;
    T const * const _end;
  public:
    explicit aContainer( T const * const begin, std::size_t count ):
      __begin{ begin }, __end{ __begin + count } { }
    auto begin_a() const { return aIterator< true, false >{ __begin }; }
    auto end_a() const { return aIterator< true, false >{ __end }; }

    auto begin_b() const { return aIterator< false, true >{ __begin }; }
    auto end_b() const { return aIterator< false, true >{ __end }; }
};

To match the style of an existing library i want to pass a reference of the iterator to a fully specialized templated function:

template< typename T, class U >
foo( /** iterator as parameter */ );

template<>
void foo< int, aClass >( /**... */ ) {
  /* ... */
}

What I want to achieve is passing the iterator into the function as a parameter like that:

/* ... */
aContainer< int, aClass, bool > container( start );
auto iterator = container.begin_a();
for( ; iterator != container.end_a(); ++iterator ) {
  foo< int, aClass >( iterator );
}
/* ... */

Is this even possible? My guesses were rejected by the compiler :/ I think, a wrapper struct could be a possible solution, but I would like to avoid that. Also a iterator base class, shadowing the template iterator class feasible possible but not preferable from a conceptual point of view.

Aucun commentaire:

Enregistrer un commentaire