Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'auto' in for each causes C3539 - Why?

I am writing a managed DLL in VC2010 (i.e. /CLR is enabled for a VC++ DLL project). Following code wouldn't compile:

System::Collections::Generic::List<int>^ my_list;
for each(auto elem in my_list)
{

}

It raises error C3539: 'auto': a template-argument cannot be a type that contains 'auto'.

I don't understand the reason. I tried compiling the same in VS2012, and it raises same error (which is not appropriate error).

Why compiler fails to deduce the type for a colleciton? The same type of code would work in C# with var keyword.

like image 653
Ajay Avatar asked Nov 11 '22 21:11

Ajay


1 Answers

First, the most importand point from the comments:

presented code does compile in VS2013 c++/cli dll .net 4.5 (Zee, 2014-05-03)

When you compile C++/CLI, which is the .NET binding for C++, you are using a different feature set of the Microsoft compiler. Whether something works either

  • when /clr is in effect
  • or, additionally, when you're using a "managed" construct (as in your code)

has nothing to to with if the "normal", native, MSVC compiler accepts it.

So as for "why": It would simply appear that auto type deduction did not work for the managed handle types in VS2010 and VS2012, but, according to Zee's comment, has then been implemented in VS2013. (A quick Search Engine check didn't find any official statement wrt. this, so I may be wrong.)

like image 189
Martin Ba Avatar answered Dec 11 '22 00:12

Martin Ba