Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring more than one variable within a single auto statement

Tags:

c++

c++11

auto

Consider

auto x=foo(), y;

Is this legal? (I would imagine it is and implies that y is of the same type as x.) While this particular example may not be very useful, consider

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); ) { 
    ...
  }
}

Here, i and j are both of the same type (because both derive from the same type of operation on the same type of objects), so this seems perfectly safe and sensible, as it avoids to declare the appropriate type (which is best done using decltype(), i.e. via deduction again).

However, intel's compiler (version 14.0.1) warns me that

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

So, what should I make of this warning? Is there any problem that can come up with this type of usage of auto?


edit The simple code snipped above does indeed not trigger the warning. However, the code which does looks very similar:

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)
  ...

(the for loop lives inside a member method of a class template)

like image 341
Walter Avatar asked Jan 13 '23 00:01

Walter


2 Answers

auto x=foo(), y;

No it's illegal.

I can't reproduce your warning since both i and j have same type. That part is legal code.

like image 100
masoud Avatar answered Jan 17 '23 14:01

masoud


The first example is illegal: all declarators must have initialisers, and y doesn't.

The second is fine: both declarators have initialisers of the same type.

I've no idea what the warning is talking about: there are no trailing return types here. Neither GCC nor Clang give any warning about your code; I don't have an Intel compiler to test with.

like image 45
Mike Seymour Avatar answered Jan 17 '23 14:01

Mike Seymour