473,659 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusing function declaration

I have to read a file containing integers into a vector. I could do
something like this:

ifstream data("file.dat" );
istream_iterato r<intbegin(data );
istream_iterato r<intend;
vector<intc(beg in,end);

Now this does what i want it to.

But I read in Scott meyer's effective C++ that if I try to get rid of
the second and the thir statement and do something like this

ifstream data("file.dat" );
vector<intc(ist ream_iterator<i nt>(data), istream_iterato r<int>());

then this wouldn't work as expected because here the second line is
treated as a function declaration where c is the function which returns
a vector<intand takes two parameters: a)istream_itera tor<intb) a
function pointer which doesnt take any arguments and return a
istream_iterato r<int>

And that is the reason why this won't work.

But a friend of mine pointed me to this paper.
http://www.open-std.org/jtc1/sc22/wg...005/n1798.html

where it says that such a declaration won't work, not because it is
considered as a function declaration but because an istream_iterato r in
this case is considered as a forward iterator and that the
implementation of the constructor for vector which takes a forward
iterator is such that it is not possible to use an istream iterator
with them. I would request you to read the paper and correct me if I
misunderstood what the author said and let me know which one is right.

Sep 6 '06 #1
6 1424
fe***********@g mail.com wrote:
I have to read a file containing integers into a vector. I could do
something like this:

ifstream data("file.dat" );
istream_iterato r<intbegin(data );
istream_iterato r<intend;
vector<intc(beg in,end);

Now this does what i want it to.

But I read in Scott meyer's effective C++ that if I try to get rid of
the second and the thir statement and do something like this

ifstream data("file.dat" );
vector<intc(ist ream_iterator<i nt>(data), istream_iterato r<int>());

then this wouldn't work as expected because here the second line is
treated as a function declaration where c is the function [...]
Wrap the argument list in a second set of parentheses:

vector<intc((is tream_iterator< int>(data), istream_iterato r<int>()));

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '06 #2
Victor,

Thanks for the reply. I know that if I wrap the arguments it would
work. But I was more curious about that paper which had a different
explanation and that is the reason why I posted it here. Two
contradicting statements and I was curious to know which one is right.

Cheers
Priya
>
Wrap the argument list in a second set of parentheses:

vector<intc((is tream_iterator< int>(data), istream_iterato r<int>()));

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '06 #3
fe***********@g mail.com wrote:
But a friend of mine pointed me to this paper.
http://www.open-std.org/jtc1/sc22/wg...005/n1798.html

where it says that such a declaration won't work, not because it is
considered as a function declaration but because an istream_iterato r in
Have you read the last sentence of the paper?

"We avoid these problems in current C++ by emulating explicit model
definitions using traits and category tags."

--
Salu2
Sep 6 '06 #4
Julian,

Aargh my bad. Thanks a lot [:)]

Cheers
Priya

Julián Albo wrote:
fe***********@g mail.com wrote:
But a friend of mine pointed me to this paper.
http://www.open-std.org/jtc1/sc22/wg...005/n1798.html

where it says that such a declaration won't work, not because it is
considered as a function declaration but because an istream_iterato r in

Have you read the last sentence of the paper?

"We avoid these problems in current C++ by emulating explicit model
definitions using traits and category tags."

--
Salu2
Sep 6 '06 #5
On Wed, 6 Sep 2006 11:03:45 -0400 in comp.lang.c++, "Victor Bazarov"
<v.********@com Acast.netwrote,
>Wrap the argument list in a second set of parentheses:

vector<intc((is tream_iterator< int>(data), istream_iterato r<int>()));
No, wrap each argument

vector<intc((is tream_iterator< int>(data)),
(istream_iterat or<int>()));

Sep 6 '06 #6
Victor Bazarov schrieb:
Wrap the argument list in a second set of parentheses:

vector<intc((is tream_iterator< int>(data), istream_iterato r<int>()));
This shouldn't work.

The comma here is the comma operator, which discards the left hand operand.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3837
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
3
2302
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void *, void*))(numeric ? numcmp : strcmp) ); I guess what interests me is the nameless function pointer and then the
5
2085
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
20
2358
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
20
1798
by: svata | last post by:
Hello there, after some time of pondering I come to some solution which would suit me best. Please correct, if I am wrong. Function has two parameters. A string array, better said a pointer to it, char *p_buf and int size. int read_name( char *p_buf, int size) { char *p_item_name_1;
4
2511
by: Paulo Matos | last post by:
Hi all, I'm trying to work out a parser for function declarations but it turns out that it is harder than I initially thought. I'm looking at 3rd Ed of Stroustrup, page 808. I'm trying to parse something like: int foo(int, int); const double *xpto(mytype *, mytype &) const; But I'm not being able to find my way around the grammar.
2
5318
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: 1>make_buildinfo.obj : error LNK2019: unresolved external symbol __imp__RegQueryValueExA@24 referenced in function _make_buildinfo2 Ask on python-list@python.org . - Josiah
7
1922
by: Luna Moon | last post by:
Hi all, I just couldn't get myself clear about the usage of "const" in front of and/or behind variables, pointers, classes, objects and functions... It's too confusing... any good clear article/tutorial that can help me? Thanks a lot!
7
1528
by: krishna | last post by:
What is the need of this syntax for initializing values, isn't this ambiguous to function call? e.g., int func_name(20); this looks like function call (of course not totally as there is no function body), but is declaration and initialization of an integer variable 'func_name', is it step towards completeness of OOP abstraction in the language, here one could say we are creating and initializing an integer object, but then again it (this...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.