Domanda

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)

È stato utile?

Soluzione 2

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.

Altri suggerimenti

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.

According to the future C++ 2014

...shall appear as one of the decl-specifiers in the decl-specifier-seq and the declspecifier- seq shall be followed by one or more init-declarators, each of which shall have a non-empty initializer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top