0

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.
Poseidon23
  • 23
  • 4

1 Answers1

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
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • After adding `typename` to the template alias, I'm getting the following: ringDeque.cpp:48:50: error: nested name specifier 'MyIterator::' for declaration does not refer into a class, class template or class template partial specialization typename MyIterator::reference MyIterator::operator*() const { – Poseidon23 Jan 23 '22 at 23:54
  • @Poseidon23 - That oversight is easy enough to fix, and I have. But do not mistake SO for a free interactive debugging session by correspondence. I will only address what you post in your question, not in comments. – StoryTeller - Unslander Monica Jan 23 '22 at 23:58
  • Got it. Why is it not possible to replace the second `RingDeque::Iterator` with the template alias? – Poseidon23 Jan 24 '22 at 00:00
  • @Poseidon23 - Language rules for declaration matching. The error mentioned what the compiler expected there. – StoryTeller - Unslander Monica Jan 24 '22 at 00:02