Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ifstream/string ctor

Reply
Thread Tools

ifstream/string ctor

 
 
Chris Forone
Guest
Posts: n/a
 
      12-16-2007
hello group,

why have i to bracket the second ctor param in the following example?

thx & hand, chris

#include <fstream>
#include <iterator>

int main()
{
std::ifstream file(__FILE__);

if (file.is_open())
{
noskipws(file);
std::string text(std::istream_iterator<char>(file),
(std::istream_iterator<char>())); // extra brackets here
}
}
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      12-16-2007
On Dec 16, 10:03 am, Chris Forone <(E-Mail Removed)> wrote:
> why have i to bracket the second ctor param in the following example?


> #include <fstream>
> #include <iterator>


> int main()
> {
> std::ifstream file(__FILE__);
>
> if (file.is_open())
> {
> noskipws(file);
> std::string text(std::istream_iterator<char>(file),
> (std::istream_iterator<char>())); // extra brackets here
> }
> }


You don't. You can bracket the first instead.

If you bracket neither, of course, you've declared a function
taking an istream_iterator<char> as first argument, a pointer to
a function returning an istream_iterator<char> as second
argument, and returning an string. Which is, of course, also
legal (and your exact code compiles with my compiler), but
probably not what you wanted. (I presume that in the code which
actually triggers the error, you tried to use text later, as if
it were a string, and not a function.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
Chris Forone
Guest
Posts: n/a
 
      12-16-2007
James Kanze schrieb:
> On Dec 16, 10:03 am, Chris Forone <(E-Mail Removed)> wrote:
>> why have i to bracket the second ctor param in the following example?

>
>> #include <fstream>
>> #include <iterator>

>
>> int main()
>> {
>> std::ifstream file(__FILE__);
>>
>> if (file.is_open())
>> {
>> noskipws(file);
>> std::string text(std::istream_iterator<char>(file),
>> (std::istream_iterator<char>())); // extra brackets here
>> }
>> }

>
> You don't. You can bracket the first instead.
>
> If you bracket neither, of course, you've declared a function
> taking an istream_iterator<char> as first argument, a pointer to
> a function returning an istream_iterator<char> as second
> argument, and returning an string. Which is, of course, also
> legal (and your exact code compiles with my compiler), but
> probably not what you wanted. (I presume that in the code which
> actually triggers the error, you tried to use text later, as if
> it were a string, and not a function.)
>
> --
> James Kanze (GABI Software) email:(E-Mail Removed)
> Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

thx,

if i understand it right, w/o brackets its a declaration of a function,
w brackets its one of the string-ctors?! in web ive seen examples w/o
brackets, but they didnt compile w g++ 4.1...

best regards, chris
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-17-2007
On Dec 16, 12:01 pm, Chris Forone <(E-Mail Removed)> wrote:
> James Kanze schrieb:


> > On Dec 16, 10:03 am, Chris Forone <(E-Mail Removed)> wrote:
> >> why have i to bracket the second ctor param in the
> >> following example?


> >> #include <fstream>
> >> #include <iterator>


> >> int main()
> >> {
> >> std::ifstream file(__FILE__);


> >> if (file.is_open())
> >> {
> >> noskipws(file);
> >> std::string text(std::istream_iterator<char>(file),
> >> (std::istream_iterator<char>())); // extra brackets here
> >> }
> >> }


> > You don't. You can bracket the first instead.


> > If you bracket neither, of course, you've declared a function
> > taking an istream_iterator<char> as first argument, a pointer to
> > a function returning an istream_iterator<char> as second
> > argument, and returning an string. Which is, of course, also
> > legal (and your exact code compiles with my compiler), but
> > probably not what you wanted. (I presume that in the code which
> > actually triggers the error, you tried to use text later, as if
> > it were a string, and not a function.)


> if i understand it right, w/o brackets its a declaration of a
> function, w brackets its one of the string-ctors?!


It is a declaration of a variable (an object) with string type.

The basic rule is that if something can be interpreted as a
declaration (rather than as an expression), then it is. So if
you write something like:
std::istream_iterator<char>(file)
it is a declaration (of a variable named file, with type
std::istream_iterator< char >), unless the context where it
appears doesn't allow declarations.

The case of:
std::istream_iterator<char>()
is more complicated, since there is nothing that could be being
declared. But there are a few contexts, such as the declaration
of a function parameter, where you do not need to name what is
being declared. In such cases, this is a declaration of a
function (and as a function parameter, the declaration of a
function is "reinterpreted" as the declaration of a pointer to a
function.

In your code above, but without the braces, if you interpret
these expressions as declarations, you end up with a legal
function declaration. Since they can be interpreted as
declarations, the standard says that they should be, and you
have a legal function declaration. If you put even one in
parentheses, however... a declaration can never appear in
parentheses, so it cannot be a declaration, and must be an
expression (an "explicit type conversion (functional notation)",
according to the standard---the explicit creation of a
temporary, in common parlance). And if even one of them is an
expression, then the complete statement cannot be a function
declaration, which means that the second can't be a declaration
either. And if they're expressions, then the statement must be
the definition of a variable, with direct initialization and two
initializers (which means that the variable will be initialized
by calling the constructor found by overload resolution for the
two expressions).

> in web i've seen examples w/o brackets, but they didnt compile
> w g++ 4.1...


They shouldn't compile anywhere.

(Note too that you'll find a lot of things on the Web. There's
no requirement that anything be correct for it to appear in a
web page.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Chris Forone
Guest
Posts: n/a
 
      12-18-2007
James Kanze schrieb:
> On Dec 16, 12:01 pm, Chris Forone <(E-Mail Removed)> wrote:
>> James Kanze schrieb:

>
>>> On Dec 16, 10:03 am, Chris Forone <(E-Mail Removed)> wrote:
>>>> why have i to bracket the second ctor param in the
>>>> following example?

>
>>>> #include <fstream>
>>>> #include <iterator>

>
>>>> int main()
>>>> {
>>>> std::ifstream file(__FILE__);

>
>>>> if (file.is_open())
>>>> {
>>>> noskipws(file);
>>>> std::string text(std::istream_iterator<char>(file),
>>>> (std::istream_iterator<char>())); // extra brackets here
>>>> }
>>>> }

>
>>> You don't. You can bracket the first instead.

>
>>> If you bracket neither, of course, you've declared a function
>>> taking an istream_iterator<char> as first argument, a pointer to
>>> a function returning an istream_iterator<char> as second
>>> argument, and returning an string. Which is, of course, also
>>> legal (and your exact code compiles with my compiler), but
>>> probably not what you wanted. (I presume that in the code which
>>> actually triggers the error, you tried to use text later, as if
>>> it were a string, and not a function.)

>
>> if i understand it right, w/o brackets its a declaration of a
>> function, w brackets its one of the string-ctors?!

>
> It is a declaration of a variable (an object) with string type.
>
> The basic rule is that if something can be interpreted as a
> declaration (rather than as an expression), then it is. So if
> you write something like:
> std::istream_iterator<char>(file)
> it is a declaration (of a variable named file, with type
> std::istream_iterator< char >), unless the context where it
> appears doesn't allow declarations.
>
> The case of:
> std::istream_iterator<char>()
> is more complicated, since there is nothing that could be being
> declared. But there are a few contexts, such as the declaration
> of a function parameter, where you do not need to name what is
> being declared. In such cases, this is a declaration of a
> function (and as a function parameter, the declaration of a
> function is "reinterpreted" as the declaration of a pointer to a
> function.
>
> In your code above, but without the braces, if you interpret
> these expressions as declarations, you end up with a legal
> function declaration. Since they can be interpreted as
> declarations, the standard says that they should be, and you
> have a legal function declaration. If you put even one in
> parentheses, however... a declaration can never appear in
> parentheses, so it cannot be a declaration, and must be an
> expression (an "explicit type conversion (functional notation)",
> according to the standard---the explicit creation of a
> temporary, in common parlance). And if even one of them is an
> expression, then the complete statement cannot be a function
> declaration, which means that the second can't be a declaration
> either. And if they're expressions, then the statement must be
> the definition of a variable, with direct initialization and two
> initializers (which means that the variable will be initialized
> by calling the constructor found by overload resolution for the
> two expressions).
>
>> in web i've seen examples w/o brackets, but they didnt compile
>> w g++ 4.1...

>
> They shouldn't compile anywhere.
>
> (Note too that you'll find a lot of things on the Web. There's
> no requirement that anything be correct for it to appear in a
> web page.)
>
> --
> James Kanze (GABI Software) email:(E-Mail Removed)
> Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

thanks a lot for this professional answer!

best regards, chris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reference to pointer used in ctor (using 'this' in ctor) Anonymous C++ 2 08-28-2007 01:10 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments