Valid signature for a template class method that returns its own type

In my specific case, a custom iterator is used, but this question is general. I'm not sure how to write a return type for this postfix increment method:

template<typename T>
struct MyIterator {
    size_t loc;

    MyIterator operator++(int) {
        MyIterator temp(*this);
        ++loc;
        return temp;
    }
};

This compiles, but also:

template<typename T>
struct MyIterator {

    size_t loc;

    MyIterator<T> operator++(int) {
        MyIterator<T> temp(*this);
        ++loc;
        return temp;
    }
};

The other two configurations also work fine (i.e. placing <T>only in one of the instances MyIterator). Is there a “right” way to write this? Does it matter?

+4
source share
1 answer

Is there a “right” way to write this?

. , . , :

template <typename T1, typename T2, typename T3> class MyClass { ... };

MyClass, MyClass<T1, T2, T3>.

?

.

, , . , : ?


, auto , skypjack.

+8

Source: https://habr.com/ru/post/1679076/


All Articles