考虑以下程序:
int main ()
{
const int e = 10;
for (decltype(e) i{0}; i < e; ++i) {
// do something
}
}
这无法使用clang(以及gcc)进行编译:
decltype.cpp:5:35: error: read-only variable is not assignable
for (decltype(e) i{0}; i < e; ++i) {
^ ~
基本上,编译器假定i
必须是const,因为e
是。
有没有一种方法可以decltype
用来获取的类型e
,但可以删除说明const
符?
我喜欢auto i = decltype(e){0};
这个。它比使用起来要简单一些type_traits
,而且我觉得它更明确地指定了您想要将变量初始化为0e
的类型的意图。
我最近一直在使用Herb的“ AAA风格”,所以这可能只是我偏见。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句