0

I have some old C++ code that I am trying to bring up to more modern times. At this stage, I'm trying to get code that compiled with Visual C++ 6.0 to now compile with VS 2003 (Microsoft Visual C++ .NET 69462-335-0000007-18915). If we can get this to compile cleanly & run properly, then we can take another step to get it into a more recent version of VS.

But I have found a case of an undeclared variable not being flagged by the C++ compiler! The code is something like this:

for (MLink *ML = m_Links.begin(); ML != m_Links.end(); ++ML)
{
  // some code here
}

for (std::map<CString,int>::iterator it = Rows.begin();
     it != Rows.end(); ++it)
{
  MLink *ML = std::find (m_Links.begin(), m_Links.end(), Type);
  // more code here
}

ML = m_Links.begin();

In the first for loop, ML is defined as a pointer within the loop statement. I understand this to mean that ML is undefined outside the loop.

In the second for loop, ML is defined as a pointer within the loop code block. Again, I understand this to mean that ML is undefined outside the loop.

But the last line references ML without declaring its type and the compiler gives no error (or even a warning). How can this be???

Note: the m_Links variable is a vector, and the begin() method changed in the standard library to return an iterator, rather than a pointer. So that problem is correctly flagged as an error on all three occurences.

Todd Hoatson
  • 123
  • 2
  • 18
  • You are "upgrading" to a 14 year old compiler? –  Nov 06 '17 at 23:00
  • Which of the 2 compilers, (VC++6 or VS 2003) fails to flag this error ? Neither compiler are particularly standards compliant though - and at least VC++6 has broken rules for the scope of variables declared in a for loop) – nos Nov 06 '17 at 23:00
  • The compiler I am using now is VS 2003. It fails to flag the error. So the new information in my post is that VS 2003 still has this problem. It was my understanding that VS 2003 had been updated to be compliant with the C++ standard... – Todd Hoatson Nov 07 '17 at 02:14

1 Answers1

2

Visual C++ 6 was released in 1998, and it was not (and could not) fully conforming to the same year C++98 standard. The variables that were declared in a for loop init statement were accessible outside the loop.

for (int i = 0; i < n; ++i) {
    ...
} 

was the same as

int i;
for (i = 0; i < n; ++i) {
    ...
} 

There was a fairly common macro those days to prevent this behavior:

#define for if(0); else for

If you still have this behavior in VS 2003 look at the /Zc:forScope compiler option.

DAle
  • 8,990
  • 2
  • 26
  • 45
  • VC++6 predated the C++98 Standard (which specified the scope of loop control variables) and was by no means unique in its treatment of such variables. –  Nov 06 '17 at 23:19
  • @NeilButterworth, that's right, but the question was about Visual C++ 6. Did I misuse the word 'specialty'? – DAle Nov 06 '17 at 23:27
  • 1
    An explanation for the confusion is that when VC++ implemented the feature, this was correct according to the then available drafts of the upcoming standard. The rules were changed before standardization, but the compiler was not. – Bo Persson Nov 06 '17 at 23:29
  • @Dale "Did I misuse the word 'specialty" - I would say so as this was not unique to VC++6 at the time. –  Nov 06 '17 at 23:34