繁体   English   中英

在单个auto语句中声明多个变量

[英]declaring more than one variable within a single auto statement

考虑

auto x=foo(), y;

这合法吗? (我想是它并且暗示yx类型相同。)虽然这个特定的例子可能不是很有用,但请考虑

template<typename some_type>
void func(some_type const&x, some_type const&y)
{
  for(auto i=std::begin(x),j=std::begin(y); i!=std::end(x) && j!=std::end(y); ) { 
    ...
  }
}

这里, ij都是相同的类型(因为它们都来自同一类型对象的相同类型的操作),所以这看起来非常安全和合理,因为它避免声明适当的类型(这是最好的完成使用decltype() ,即再次扣除)。

但是,intel的编译器(版本14.0.1)警告我

warning #3373: nonstandard use of "auto" to both deduce the type from an initializer and to announce a trailing return type

那么,我该怎么做这个警告呢? 是否有任何问题可以提出这种类型的auto使用?


编辑上面剪断的简单代码确实没有触发警告。 但是,代码看起来非常相似:

struct Neighbour { ... };
typedef std::vector<Neighbour> NeighbourList;
NeighbourList const&A;
NeighbourList const&B;
...
const auto K = std::max(A.size(),B.size());
auto k = 0*K;
for(auto iA=A.begin(),iB=B.begin(); k!=K; ++k,++iA,++iB)
  ...

(for循环位于类模板的成员方法内)

auto x=foo(), y;

不,这是非法的。

我不能重现你的警告,因为ij都有相同的类型。 那部分是合法代码。

第一个例子是非法的:所有声明者都必须有初始化者,而y则不是。

第二个是好的:两个声明者都有相同类型的初始化器。

我不知道警告在说什么:这里没有尾随返回类型。 GCC和Clang都没有对您的代码发出任何警告; 我没有要测试的英特尔编译器。

根据未来的C ++ 2014

...应该作为decl-specifier-seq中的decl-specifiers之一出现,而declspecifier-seq后面应该跟一个或多个init-declarator,每个init-declarator都有一个非空的初始值设定项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM