Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 806
Hatted Rooster Avatar asked Jan 05 '16 18:01

Hatted Rooster


2 Answers

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;
    }
}
like image 161
R Sahu Avatar answered Nov 20 '22 19:11

R Sahu


In C++14, you do not need decltype() at all. Following is correct per C++14 (as far as I know, MSVC does not fully support C++14):

   auto internal_peek()
    {
        if (m_activeStackSize) // peek from stack
        {
            auto itr = data.end();
            std::advance(itr, -1);
            return itr;
        }
        else //peek from queue
        {
            auto itr = data.begin();
            return itr;
        }
    }
like image 30
SergeyA Avatar answered Nov 20 '22 19:11

SergeyA