Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 577
Jonathan Avatar asked Jun 18 '14 23:06

Jonathan


1 Answers

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
};
like image 80
Praetorian Avatar answered Oct 24 '22 15:10

Praetorian