473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

prototype declaration not variable definition

I've seen cases before where it was intended to write a variable declaration
but a function prototype was written instead (e.g. X x();) but I've come
accross a new version of this (new to me at least) and I want to check I've
understood it correctly.

The code I wrote was.

std::istringstream buf("abc");
std::vector<char> vec(std::istream_iterator<char>(buf),
std::istream_iterator<char>());

To my surprise vec was interpreted as a function prototype. After a bit of
thought here's how I see it

std::istream_iterator<char>(buf)

is a parameter declaration with std::istream_iterator<char> as the type and
buf as the dummy parameter name, and also a superfluous pair of parens. So
far so good, the bit that I couldn't work out was how

std::istream_iterator<char>()

was being interpreted as a parameter declaration. Then it occured to me that
in function declarations you are allowed to omit the dummy parameter name,
so that std::istream_iterator<char>() could be interpreted as parameter
declaration with superfluous parens surrounding the missing dummy parameter
name!

Is this a correct interpretation? Or am I barking?

john
Jul 19 '05 #1
4 4914
"John Harrison" <jo*************@hotmail.com> wrote...
I've seen cases before where it was intended to write a variable declaration but a function prototype was written instead (e.g. X x();) but I've come
accross a new version of this (new to me at least) and I want to check I've understood it correctly.

The code I wrote was.

std::istringstream buf("abc");
std::vector<char> vec(std::istream_iterator<char>(buf),
std::istream_iterator<char>());

To my surprise vec was interpreted as a function prototype. After a bit of
thought here's how I see it

std::istream_iterator<char>(buf)

is a parameter declaration with std::istream_iterator<char> as the type and buf as the dummy parameter name, and also a superfluous pair of parens. So
far so good, the bit that I couldn't work out was how

std::istream_iterator<char>()

was being interpreted as a parameter declaration. Then it occured to me that in function declarations you are allowed to omit the dummy parameter name,
so that std::istream_iterator<char>() could be interpreted as parameter
declaration with superfluous parens surrounding the missing dummy parameter name!

Is this a correct interpretation? Or am I barking?


You're absolutely correct.

Victor
Jul 19 '05 #2
John Harrison wrote:
I've seen cases before where it was intended to write a variable declaration
but a function prototype was written instead (e.g. X x();) but I've come
accross a new version of this (new to me at least) and I want to check I've
understood it correctly.

The code I wrote was.

std::istringstream buf("abc");
std::vector<char> vec(std::istream_iterator<char>(buf),
std::istream_iterator<char>());

To my surprise vec was interpreted as a function prototype. After a bit of
thought here's how I see it

std::istream_iterator<char>(buf)

is a parameter declaration with std::istream_iterator<char> as the type and
buf as the dummy parameter name, and also a superfluous pair of parens. So
far so good, the bit that I couldn't work out was how

std::istream_iterator<char>()

was being interpreted as a parameter declaration. Then it occured to me that
in function declarations you are allowed to omit the dummy parameter name,
so that std::istream_iterator<char>() could be interpreted as parameter
declaration with superfluous parens surrounding the missing dummy parameter
name!

Is this a correct interpretation? Or am I barking?
john


Close. The second parameter of vec is a pointer to a function taking no
arguments and returning std::istream_iterator <char>.

#include <sstream>
#include <vector>
#include <iterator>

std::istream_iterator <char> b;
std::istream_iterator <char> f ();

int main ()
{
std::istringstream buf ("abc");
std::vector <char> vec (std::istream_iterator <char> (buf),
std::istream_iterator <char> ());

sizeof vec (b, f); // use the declarations; no error
}

Regards,
Buster.
Jul 19 '05 #3
> >
Is this a correct interpretation? Or am I barking?
> john


Close. The second parameter of vec is a pointer to a function taking no
arguments and returning std::istream_iterator <char>.

#include <sstream>
#include <vector>
#include <iterator>

std::istream_iterator <char> b;
std::istream_iterator <char> f ();

int main ()
{
std::istringstream buf ("abc");
std::vector <char> vec (std::istream_iterator <char> (buf),
std::istream_iterator <char> ());

sizeof vec (b, f); // use the declarations; no error
}

Regards,
Buster.


That would be

std::vector <char> vec (std::istream_iterator <char> (buf),
std::istream_iterator <char> (*)());

wouldn't it?

john
Jul 19 '05 #4

"Buster Copley" <bu****@none.com> wrote in message
news:bg**********@newsg2.svr.pol.co.uk...
Close. The second parameter of vec is a pointer to a function taking no
arguments and returning std::istream_iterator <char>.

That would be

std::vector <char> vec (std::istream_iterator <char> (buf),
std::istream_iterator <char> (*)());

wouldn't it?

john


Yes, that would work too.
Buster


But my point was I didn't realise

void f(int ());

was a synonym for

void f(int (*)());

How long has that been part of the language?

And how is that interpretation of void f(int()) preferred over mine. Isn't
that another ambiguity?

john
Jul 19 '05 #5

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

Similar topics

21
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...
23
by: Allin Cottrell | last post by:
Thomas Heinz wrote (in re. gcc compilation of this erroneous program): $ cat test.c int f(int); int f(); int f() {return 0;} int main (void) { return 0; }
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
2
by: vlsidesign | last post by:
Here is my a portion of my program: #include <stdio.h> main() { int fahr, celsius; int lower, upper, step; int fc_conv(int fahr); ... snip ... }
4
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
7
by: blangela | last post by:
I am currently taking a course and one of the example programs showed a function prototype something like: extern void func(); I asked why the "extern" was necessary. Someone in the class...
4
by: vshenoy | last post by:
Hi Guys, I was going through gdbm-1.8.3 source (http://ftp.gnu.org/gnu/gdbm/ gdbm-1.8.3.tar.gz) and found this strange thing : all the exposed functions of gdbm work with GDBM_FILE pointer...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.