Click here to Skip to main content
13,299,495 members (57,209 online)
Rate this:
Please Sign up or sign in to vote.
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.
Posted 5-Dec-12 0:30am
Richard MacCutchan 5-Dec-12 6:45am
but MS says no.

What exactly do they say, and where do they say it?
Member 9586495 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.
Member 9586495 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.
Member 9586495 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171207.1 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100