Inadvertent function declarations

P

Pete C

As I understand it, any statement that *could* be interpreted as a
function declaration, must be. But what is the crucial difference
between these two snippets:

/***************************************/

#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
int retval;
istringstream stream(string(argv[1]));
stream >> retval; // error, line above was a fn declaration
return retval;
}

/***************************************/

#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
int retval;
const string str(argv[1]);
istringstream stream(str);
stream >> retval; // works OK
return retval;
}

/***************************************/

So, why did the version trying to use a temporary std::string fail?
Why does referencing a parameter (argv[1]) not make the statement
unambiguous?

And finally, an alternate version:
istringstream stream = istringstream(string(argv[1]));
fails for lack of an available copy constructor. I thought that with
this kind of initialisation, no copying takes place. What are the rules
here?

I'm using g++ 4.0.1, if it makes a difference.

Thanks...
 
B

bart.kowalski

Pete said:
As I understand it, any statement that *could* be interpreted as a
function declaration, must be. But what is the crucial difference
between these two snippets:
[snip]
So, why did the version trying to use a temporary std::string fail?
Why does referencing a parameter (argv[1]) not make the statement
unambiguous?

What is the error you're getting?
And finally, an alternate version:
istringstream stream = istringstream(string(argv[1]));
fails for lack of an available copy constructor. I thought that with
this kind of initialisation, no copying takes place. What are the rules
here?

The compiler might optimize the code so that the copy ctor is never
invoked, but it is still required for the code to compile.


Bart.
 
P

Pete C

What is the error you're getting?

On the line with the insertion operator, I get:
error: invalid operands of types 'std::istringstream ()(std::string*)'
and 'int' to binary 'operator>>'

I don't see where the type std::string* came from...
Thanks!
 
B

bart.kowalski

Pete said:
istringstream stream(string(argv[1]));
On the line with the insertion operator, I get:
error: invalid operands of types 'std::istringstream ()(std::string*)'
and 'int' to binary 'operator>>'

I don't see where the type std::string* came from...

The compiler seems to parse this as:

istringstream stream(string argv[1]);

The parentheses add nothing. It's just a declaration of a function
taking an array of string. A quite contorted legacy of the C language.

If you don't want to use a temporary object try:

istringstream stream(string(*(argv+1)));


Bart.
 
P

Pete C

Thanks, I sort of see now. Still not 100% sure why the parentheses
didn't make a difference, but I see where the compiler's coming from
now.
A prettier solution (using a not-so-obscure C legacy) seems to be:
istringstream stream((string)argv[1]);
Thanks for the help.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top