你能在对象函数上声明一个带有decltype的成员变量吗?

时间:2014-06-18 23:02:09

标签: c++ c++11 decltype

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

在Visual Studio 2013上我收到编译器错误C2228:'。begin'左边必须有class / struct / union。

这是有效的C ++ 11代码,如果没有,有没有办法在没有为迭代器输入长模板类型的情况下执行此操作?

我认为decltype应该工作的逻辑是编译器可以绝对看到函数签名,所以我认为你可以根据它的返回类型声明一个变量。

1 个答案:

答案 0 :(得分:5)

您的代码有效。这是known VS bug。链接错误报告中的示例类似:

#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
};
相关问题