prototype declaration not variable definition

J

John Harrison

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
 
V

Victor Bazarov

John Harrison said:
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
 
B

Buster Copley

John said:
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.
 
J

John Harrison

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
 
J

John Harrison

Buster Copley said:
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top