Topic: even MORE weirder ...


Author: Dietmar Kuehl <dietmar.kuehl@claas-solutions.de>
Date: 1999/11/03
Raw View
Hi,
In article <3818076C.DA20B167@apfel.de>,
  "Witold Kaminski, Student" <kaminski@apfel.de> wrote:
> >     std::vector<int> buf(std::istream_iterator<int>(in),
> >                          std::istream_iterator<int>);

The above line declares a function called buf taking two iterators as
arguments and returning a 'vector<int>'.

> The error is ok, because buf isn't declared, because there is an
> error in the declaration, use instead:

Actually, there is no error in the declaration but it is not what was
desired: Instead of the declaration of a function, a definition of a
'vector<int>' is sought.

> std::vector<int> buf(std::istream_iterator<int>(in),
>                          std::istream_iterator<int>());

Which, of course, still declares a function although one with a
different type: The second argument is no a pointer to a function taking
no arguments and returing an 'istream_iterator<int>'.

This line might as well define a 'vector<int>'. However, since the line
might be both a declaration or a definition, the compiler chooses to use
the declaration according to a corresponding rule in the standard. To
remove the ambiguity, you can put one of the arguments into parenthesis:

  std::vector<int> buf((std::istream_iterator<int>(in)),
                       std::istream_iterator<int>());
--
<mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>


Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Darin Adler <darin@bentspoon.com>
Date: 1999/10/28
Raw View
Jeet Sukumaran <jeet_sukumaran@my-deja.com> wrote:

> Now here's something from the twilight zone:
>
> void foo(istream& in) {
>
> std::vector<int> buf(std::istream_iterator<int>(in),
> std::istream_iterator<int>);
>
> buf.push_back(3);  // for example
> // error: buf not declared in this scope!!!???
>
> }

This is a classic problem with C++ syntax. This line

    std::vector<int> buf(std::istream_iterator<int>(in),
        std::istream_iterator<int>);

declares a function named buf that returns a std::vector<int>, has a first
parameter of type std::istream_iterator<int> named in, and a second
parameter of type std::istream_iterator<int>.

You wanted to define a std::vector<int> object rather than declare a
function.

There are many ways to avoid the problem -- basically anything that makes it
not look to the compiler like a function declaration. One example is

    std::vector<int> buf((std::istream_iterator<int>(in)),
        std::istream_iterator<int>);

where the parentheses do the trick.

    -- Darin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Witold Kaminski, Student" <kaminski@apfel.de>
Date: 1999/10/28
Raw View
Hi !

> Well THAT clears things up!
>
> Now here's something from the twilight zone:
>
> void foo(istream& in) {
>
>     std::vector<int> buf(std::istream_iterator<int>(in),
>                          std::istream_iterator<int>);
>
>     buf.push_back(3);  // for example
>     // error: buf not declared in this scope!!!???
>
> }

The error is ok, because buf isn't declared, because there is an error in
the declaration,
use instead:
//...
std::vector<int> buf(std::istream_iterator<int>(in),
                         std::istream_iterator<int>());
//...

Bye,
        Witold Kaminski
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/10/28
Raw View
Jeet Sukumaran wrote:
>
> In article <38146B27.9CC8BCF5@wizard.net>,
>   James Kuyper <kuyper@wizard.net> wrote:
> > It's templated on the iterator type used as input.
>
> Well THAT clears things up!
>
> Now here's something from the twilight zone:
>
> void foo(istream& in) {
>
>     std::vector<int> buf(std::istream_iterator<int>(in),
>                          std::istream_iterator<int>);
>
>     buf.push_back(3);  // for example
>     // error: buf not declared in this scope!!!???
>
> }

[...]

> What gives?  Can you or anyone else figure out what is going on?

Your first line doesn't define an object of type std::vector<int>
with name buf, but it declares a function named buf
which takes two arguments of type std::istraem_iterator<int>
(the first one beeing named "in", the second one unnamed)
and returns a std::vector<int>.

Whenever something can be parsed as function declaration,
it is, even if it could be parsed as object definition as
well.

BTW, your's can't even be parsed as object definition - you'd
have to add "()" to the second argument for that. However,
this wouldn't help (since then the second argument would be
parsed as type "function taking no arguments and returning
std::istream_iterator<int>", which gets adjusted to a pointer
to such a function, and therefore it's a legal function
declaration as well.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Jeet Sukumaran <jeet_sukumaran@my-deja.com>
Date: 1999/10/27
Raw View
In article <38146B27.9CC8BCF5@wizard.net>,
  James Kuyper <kuyper@wizard.net> wrote:
> It's templated on the iterator type used as input.

Well THAT clears things up!

Now here's something from the twilight zone:

void foo(istream& in) {

    std::vector<int> buf(std::istream_iterator<int>(in),
                         std::istream_iterator<int>);

    buf.push_back(3);  // for example
    // error: buf not declared in this scope!!!???

}

As you can see, the problem is if buf is declared and defined as above,
it does not exist in foo's scope!  I get compiler errors telling me
that left-hand side requires struct/class etc..  With both Borland and
gcc (haven't tried it with MSVC).  Same with virtually any construct
that can be declared in that way (e.g. stringstream and so on).

The following works --

void foo(istream& in) {

    std::istream_iterator<int> begin(in);
    std::istream_iterator<int> end;
    std::vector<int> buf(begin, end);

    buf.push_back(3);  // for example
    // OK

}



What gives?  Can you or anyone else figure out what is going on?

-- jeet


Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]