Search code examples
c++c++14decltype

Using decltype to return iterator


I have this class:

template <typename T>
class Hybrid
{
public:
    Hybrid() : m_activeStackSize(0) {}

private:
    std::list<T> data;

    size_t m_activeStackSize;

    auto internal_peek() -> decltype(data)::iterator
    {
        if (m_activeStackSize) // peek from stack
        {
            decltype(data)::iterator itr = data.end();
            std::advance(itr, -1);
            return itr;
        }
        else //peek from queue
        {
            decltype(data)::iterator itr = data.begin();
            return itr;
        }
    }
};

When trying to compile this on Microsoft Visual Studio 2015 I get :

main.cpp(12): error C3646: 'iterator': unknown override specifier

I don't understand why it won't let me return an iterator of type std::list<T> while the body code :

decltype(data)::iterator itr = data.end();

And

decltype(data)::iterator itr = data.begin();

Compile successfully.

How would I successfully return an std::list iterator using decltype explicitly?

Removing -> decltype(data)::iterator does compile successfully.

Edit: Compiling with GCC and adding typename for every decltype compiles fine, MSVC still errors out.


Solution

  • decltype(data)::iterator is a dependent type. Hence, you must use typename.

    auto internal_peek() -> typename decltype(data)::iterator
                           //  ^^^^^ here
    {
        if (m_activeStackSize) // peek from stack
        {
            typename decltype(data)::iterator itr = data.end();
            // ^^^^^ and here
            std::advance(itr, -1);
            return itr;
        }
        else //peek from queue
        {
            typename decltype(data)::iterator itr = data.begin();
            // ^^^^^ and here
            return itr;
        }
    }
    

    Possible work around for MSVC.

    // Declare iterator as a type.
    using iterator = typename std::list<T>::iterator;
    
    iterator internal_peek()
    {
        if (m_activeStackSize) // peek from stack
        {
            iterator itr = data.end();
            std::advance(itr, -1);
            return itr;
        }
        else //peek from queue
        {
            iterator itr = data.begin();
            return itr;
        }
    }