Poseidon23
Poseidon23

Reputation: 23

Aliasing a nested class of a template

I have the following in a header file:

template <typename T>
class RingDeque {
  ...
  struct Iterator {
    using reference = T&;
    reference operator*() const;
  }
}

and I want to implement the operator*() function in the cpp file. I currently have the following in the cpp file:

template <typename T>
typename RingDeque<T>::Iterator::reference RingDeque<T>::Iterator::operator*()
    const {
  ... some implementation
}

Is there anyway I can alias a type to RingDeque<T>::Iterator? Perhaps something like this?

template <typename T>
using MyIterator = RingDeque<T>::Iterator;
template <typename T>
MyIterator::reference MyIterator::operator*()
    const {
  ... some implementation
}

I've tried many variations on this, and I haven't been able to substitute the types in the implementation of the method. For example, with the following code:

template <typename T>
using MyIterator = typename RingDeque<T>::Iterator;

template <typename T>
RingDeque<T>::Iterator::reference MyIterator::operator*() const {
  return this->_data[0];
}

I get the following error message:

ringDeque.cpp:49:1: error: missing 'typename' prior to dependent type name 'RingDeque<T>::Iterator::reference'
RingDeque<T>::Iterator::reference MyIterator::operator*() const {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typename 
ringDeque.cpp:49:35: error: 'MyIterator' is not a class, namespace, or enumeration
RingDeque<T>::Iterator::reference MyIterator::operator*() const {
                                  ^
ringDeque.cpp:46:1: note: 'MyIterator' declared here
using MyIterator = typename RingDeque<T>::Iterator;
^
2 errors generated.

Upvotes: 0

Views: 184

Answers (1)

I would just use a trailing return type and be done with it.

template <typename T>
auto RingDeque<T>::Iterator::operator*() const -> reference {
  //... some implementation
}

If you really want to use an alias template, then you mustn't forget it's also, as the name suggests, a template.

template <typename T>
using MyIterator = RingDeque<T>::Iterator;
template <typename T>
typename MyIterator<T>::reference RingDeque<T>::Iterator::operator*()
    const {
  //... some implementation
}

Upvotes: 1

Related Questions