Bug in compiler?

D

Daniel T.

In my compiler, the following works and does as expected:

#include <iterator>
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
using namespace std;
istream_iterator<char> inBegin(cin);
istream_iterator<char> inEnd;
vector<char> vec( inBegin, inEnd );
copy( vec.begin(), vec.end(), ostream_iterator<char>(cout, ", ") );
cout << '\n';
}

Whereas the following doesn't compile:

#include <iterator>
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
using namespace std;
vector<char> vec( istream_iterator<char>(cin),
istream_iterator<char>() );
copy( vec.begin(), vec.end(), ostream_iterator<char>(cout, ", ") );
cout << '\n';
}

main.cpp:11: error: request for member `begin' in
`vec(std::istream_iterator<char, char, std::char_traits<char>,
ptrdiff_t>, std::istream_iterator<char, char, std::char_traits<char>,
ptrdiff_t> (*)())', which is of non-aggregate type `std::vector<char,
std::allocator<char> > ()(std::istream_iterator<char, char,
std::char_traits<char>, ptrdiff_t>, std::istream_iterator<char, char,
std::char_traits<char>, ptrdiff_t> (*)())'

Is this a bug in the compiler, or something else?
 
S

Sumit Rajan

Daniel T. said:
#include <iterator>
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
using namespace std;
vector<char> vec( istream_iterator<char>(cin),
istream_iterator<char>() );

The above line changes to:
vector said:
copy( vec.begin(), vec.end(), ostream_iterator<char>(cout, ", ") );
cout << '\n';
}

Is this a bug in the compiler, or something else?

No, the extra set of parenthesis is required.

Regards,
Sumit.
 
S

Sumit Rajan

Sumit Rajan said:
The above line changes to:
vector<char> vec( istream_iterator<char>(cin),


No, the extra set of parenthesis is required.

Sorry, I should have explained further.

This is because the compiler could consider vec to be a function declaration
of a function that returns a vector<char>. It would take two parameters --
the first one being an istream_iterator<char> and the second one being a
unnamed function type taking no arguments and returning an
istream_iterator<char>.

Regards,
Sumit.
 
D

Daniel T.

"Sumit Rajan said:
The above line changes to:


No, the extra set of parenthesis is required.

Still doesn't work...

int main()
{
using namespace std;
vector<char> vec( istream_iterator<char>(cin),
(istream_iterator<char>()) ); // extra parens
copy( vec.begin(), vec.end(), ostream_iterator<char>(cout, ", ") );
cout << '\n';
}

main.cpp:27: error: parse error before `)' token

The error points to the vector<char> line...
 
S

Sumit Rajan

Daniel T. said:
Still doesn't work...

int main()
{
using namespace std;
vector<char> vec( istream_iterator<char>(cin),
(istream_iterator<char>()) ); // extra parens
copy( vec.begin(), vec.end(), ostream_iterator<char>(cout, ", ") );
cout << '\n';
}

Can you tell me which compiler you are using? I've managed to compile it
using VC++ 8.0, Comeau C++ , mingw 3.4.2 and VC++ 7.1.

Regards,
Sumit.
 
V

Victor Bazarov

Daniel said:
Still doesn't work...

int main()
{
using namespace std;
vector<char> vec( istream_iterator<char>(cin),
(istream_iterator<char>()) ); // extra parens
copy( vec.begin(), vec.end(), ostream_iterator<char>(cout, ", ") );
cout << '\n';
}

main.cpp:27: error: parse error before `)' token

The error points to the vector<char> line...

Are you sure? I just tested your code with Comeau online and it compiles
fine. The only difference from the originally posted code is the added
parentheses around the second argument passed to construct 'vec' object.

Could it be you're using a faulty compiler?

V
 
D

Daniel T.

"Sumit Rajan said:
Can you tell me which compiler you are using? I've managed to compile it
using VC++ 8.0, Comeau C++ , mingw 3.4.2 and VC++ 7.1.

I'm using apples Xcode IDE which uses GCC 3.3 on the back-end.
 
T

Thomas Tutone

Daniel said:
I'm using apples Xcode IDE which uses GCC 3.3 on the back-end.

<OT>

gcc's standard conformance got much better starting with gcc 3.4.

Quoting from http://gcc.gnu.org/gcc-3.4/changes.html:

"G++ is now much closer to full conformance to the ISO/ANSI C++
standard. This means, among other things, that a lot of invalid
constructs which used to be accepted in previous versions will now be
rejected. It is very likely that existing C++ code will need to be
fixed. This document lists some of the most common issues. A
hand-written recursive-descent C++ parser has replaced the YACC-derived
C++ parser from previous GCC releases. The new parser contains much
improved infrastructure needed for better parsing of C++ source codes,
handling of extensions, and clean separation (where possible) between
proper semantics analysis and parsing. The new parser fixes many bugs
that were found in the old parser."

Sounds like it's time for you to upgrade.

</OT>

Best regards,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top