* Declarations in a for loop
@ 2005-05-18 15:57 Fred Labrosse
2005-05-18 16:04 ` Eljay Love-Jensen
2005-05-18 16:08 ` DPalao
0 siblings, 2 replies; 5+ messages in thread
From: Fred Labrosse @ 2005-05-18 15:57 UTC (permalink / raw)
To: gcc-help
All,
The following doesn't compile (with different messages depending on the
order in which I declare things:
#include <vector>
main()
{
std::vector<double> doubles;
double aDouble;
for (int index = 0,
std::vector<double>::iterator doublesIter = doubles.begin();
doublesIter != doubles.end();
++doublesIter, ++index)
{
if (aDouble == *doublesIter)
return(index);
}
}
However, if I declare index and doublesIter before the for, then all
works fine. Is that a bug?
Fred
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Declarations in a for loop
2005-05-18 15:57 Declarations in a for loop Fred Labrosse
@ 2005-05-18 16:04 ` Eljay Love-Jensen
2005-05-18 16:36 ` Fred Labrosse
2005-05-18 16:08 ` DPalao
1 sibling, 1 reply; 5+ messages in thread
From: Eljay Love-Jensen @ 2005-05-18 16:04 UTC (permalink / raw)
To: Fred Labrosse, gcc-help
Hi Fred,
>However, if I declare index and doublesIter before the for, then all works fine. Is that a bug?
Nope, not a bug. What you have here is a case of bad C++ code.
#1
int x, y; // This is good.
#2
int x; std::vector<double>::iterator y; // This is good.
#3
std::vector<double>::iterator x, y; // This is good.
#4
int x, std::vector<double>::iterator y; // This is not good.
You can't put #2 in a for-loop initialization expression (since it's two expressions).
#1 and #3 won't do what you want.
#4 isn't C++, whether inside or outside of a for-loop initialization expression..
HTH,
--Eljay
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Declarations in a for loop
2005-05-18 15:57 Declarations in a for loop Fred Labrosse
2005-05-18 16:04 ` Eljay Love-Jensen
@ 2005-05-18 16:08 ` DPalao
2005-05-18 16:36 ` Fred Labrosse
1 sibling, 1 reply; 5+ messages in thread
From: DPalao @ 2005-05-18 16:08 UTC (permalink / raw)
To: Fred Labrosse; +Cc: gcc-help
El Miércoles, 18 de Mayo de 2005 17:56, Fred Labrosse escribió:
> All,
>
> The following doesn't compile (with different messages depending on the
> order in which I declare things:
>
> #include <vector>
>
> main()
> {
> std::vector<double> doubles;
> double aDouble;
>
> for (int index = 0,
> std::vector<double>::iterator doublesIter = doubles.begin();
> doublesIter != doubles.end();
> ++doublesIter, ++index)
> {
> if (aDouble == *doublesIter)
> return(index);
> }
> }
>
> However, if I declare index and doublesIter before the for, then all
> works fine. Is that a bug?
>
> Fred
As far as I know if you declare several variables in the so-called
init-statement they have to share the type.
Regards
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Declarations in a for loop
2005-05-18 16:08 ` DPalao
@ 2005-05-18 16:36 ` Fred Labrosse
0 siblings, 0 replies; 5+ messages in thread
From: Fred Labrosse @ 2005-05-18 16:36 UTC (permalink / raw)
To: gcc-help
On Wed, 18 May 2005 18:07:58 +0200
DPalao <dpalao@gmail.com> wrote:
>
> As far as I know if you declare several variables in the so-called
> init-statement they have to share the type.
This seems to be the case indeed.
Thanks.
Fred
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Declarations in a for loop
2005-05-18 16:04 ` Eljay Love-Jensen
@ 2005-05-18 16:36 ` Fred Labrosse
0 siblings, 0 replies; 5+ messages in thread
From: Fred Labrosse @ 2005-05-18 16:36 UTC (permalink / raw)
To: gcc-help
On Wed, 18 May 2005 11:06:23 -0500
Eljay Love-Jensen <eljay@adobe.com> wrote:
> Hi Fred,
>
> >However, if I declare index and doublesIter before the for, then all
> >works fine. Is that a bug?
>
> Nope, not a bug. What you have here is a case of bad C++ code.
>
> #1
> int x, y; // This is good.
>
> #2
> int x; std::vector<double>::iterator y; // This is good.
>
> #3
> std::vector<double>::iterator x, y; // This is good.
>
> #4
> int x, std::vector<double>::iterator y; // This is not good.
>
> You can't put #2 in a for-loop initialization expression (since it's
> two expressions).
>
> #1 and #3 won't do what you want.
>
> #4 isn't C++, whether inside or outside of a for-loop initialization
> expression..
But you can have:
for (int i, int j; ...)
Seen examples of that in Josuttis' OO programming in C++ (while I was
verifying whether I could have several declerations in the for).
Fred
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-05-18 16:36 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-18 15:57 Declarations in a for loop Fred Labrosse
2005-05-18 16:04 ` Eljay Love-Jensen
2005-05-18 16:36 ` Fred Labrosse
2005-05-18 16:08 ` DPalao
2005-05-18 16:36 ` Fred Labrosse
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).