Click here to Skip to main content
14,865,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My actual programming workbench is MS Visual Studio C++ 2010 SP1Rel.

I want to code something like:

void print_vector(vector <int>& vec)
    for (auto &elem : vec)
        cout << elem << " ";

but MS says no.

Is it really necessary to upgrade to VS 2012 or is there another way?

Thanks in advance.
Richard MacCutchan 5-Dec-12 6:45am
but MS says no.

What exactly do they say, and where do they say it?
sja63 5-Dec-12 7:11am
Thanks for your response.

I get compiler errors in line
for (auto &elem : vec)

C2143: Syntax error: There must be an ',' before ':'
C2530: 'elem': References must be initialized
C3531: "elem": A symbol, which contains "auto" must have an initializer.

(translated from German by me)
Albert Holguin 5-Dec-12 20:55pm
By the way, if you want to respond to someone's post use the "Reply" link in the top right of the post you're responding to. If you respond using that link, the user will be alerted that you responded, otherwise he won't notified at all.
sja63 5-Dec-12 7:16am
Perhaps it is not the problem of "auto",
but the MS compiler does not yet support
the new Range-For?
ThatsAlok 5-Dec-12 9:36am
you might want to read this
Maximilien 5-Dec-12 9:59am
I think the "auto" keyword is only implemented in VS2012.
sja63 5-Dec-12 10:32am
"Auto" is an old C/C++ keyword which I have never used.
It has a revival in C++11 and I am trying to understand
its new funcionality.
But it is already supported by VS 2010 in the new way.

It appears that 2010 doesn't support what you're trying to do.

Member 9586495 appears to have the right answer from what I can see.

See the following for usage in VS 2010:[^]
As Member 9586495 already noted, is not the auto keyword usage the problem. It is instead the range-based for[^] construct (supported by VS 2012, but not supported by VS 2010, see, for instance: "Does MSVC10 Visual Studio 2010 support C++ range based loops"[^] at Stack Overflow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900