可以在对象函数上使用decltype声明成员变量吗?
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;
};
在Visual Studio 2013上,我得到一个编译器错误C2228: left of '。Begin '必须有class/struct/union。
这是有效的c++ 11代码,如果不是,有没有一种方法可以在不为迭代器输入长模板类型的情况下做到这一点?
我认为decltype应该工作的逻辑是编译器绝对可以看到函数签名,所以我认为你可以根据它的返回类型声明一个变量。
您的代码有效。这是一个已知的VS bug。链接的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
};
相关文章:
- DECLTYPE 未在此范围内声明
- C++11: "decltype(1+2)"声明 xvalue 还是 prvalue?
- 在 C++14 中使用 decltype(auto) 声明静态数据成员
- 使用 decltype 的条件声明类型
- 如何将 decltype 应用于运算符 [] 声明中的成员函数
- 什么时候应该使用 decltype(x) 而不是 auto 来声明变量的类型
- 使用 decltype() 声明函数签名
- 做 decltype(c) e;和 decltype((c)) f;声明不同的类型
- 在声明后对成员函数定义使用decltype
- C++11 decltype:如何声明指针指向的类型
- 如果我们使用 "decltype(auto) f()" 作为函数声明,定义中带有"decltype(f()) result",会发生什么?
- 使用decltype声明返回类型
- `decltype `作为模板函数声明中模板类型规范的一部分
- 类方法声明中的 decltype:在声明成员之前使用时出错"referenced"
- 使用auto、decltype、=、[]声明函数
- 使用函子的返回类型来声明模板方法的返回类型,不需要decltype
- 是成员声明`decltype(name)name;`在第一个名称引用封闭作用域的本地结构中允许
- 可以在对象函数上使用decltype声明成员变量吗?
- 如何通过decltype声明迭代器的值
- 是否可以使用decltype来确定前向声明的模板类的成员函数的返回类型?