Search code examples
c++c++11decltype

Can you declare a member variable with decltype on an object function?


struct Example
{
    boost::tokenizer<boost::char_separator<char>> tokens;
    decltype (tokens.begin()) i;
};

On Visual Studio 2013 I'm getting a compiler error C2228: left of '.begin' must have class/struct/union.

Is this valid C++11 code, if not, is there a way to do this without typing the long templated type for the iterator?

My logic for thinking decltype should work is that the compiler can absolutely see the function signature, so I thought you could declare a variable based on its return type.


Solution

  • Your code is valid. This is a known VS bug. The example in the linked bug report is similar:

    #include <list>
    
    struct used {
        int bar;
    };
    struct wrap {
        used u;
        auto foo() -> decltype( u.bar ) { return u.bar; }   // works
        decltype( u.bar ) x;                                // error C2228
        std::list< decltype( u.bar ) > items;               // error C2228
    };