473,386 Members | 1,702 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,386 software developers and data experts.

ifstream/string ctor

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
}
}
Dec 16 '07 #1
4 3808
On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwrote:
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<charas first argument, a pointer to
a function returning an istream_iterator<charas 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:ja*********@gmail.com
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
Dec 16 '07 #2
James Kanze schrieb:
On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwrote:
>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<charas first argument, a pointer to
a function returning an istream_iterator<charas 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:ja*********@gmail.com
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
Dec 16 '07 #3
On Dec 16, 12:01 pm, Chris Forone <4...@gmx.atwrote:
James Kanze schrieb:
On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwrote:
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<charas first argument, a pointer to
a function returning an istream_iterator<charas 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:ja*********@gmail.com
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
Dec 17 '07 #4
James Kanze schrieb:
On Dec 16, 12:01 pm, Chris Forone <4...@gmx.atwrote:
>James Kanze schrieb:
>>On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwrote:
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<charas first argument, a pointer to
a function returning an istream_iterator<charas 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:ja*********@gmail.com
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
Dec 18 '07 #5

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

Similar topics

2
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
6
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
4
by: Lingyun Yang | last post by:
hi every one, I meet a compile error in my small homework program: Can I innitialize ifstream with a string? or I must come back to char* style string? Thank you! Lingyun
4
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
6
by: Gary Wessle | last post by:
hi I have a code, the part which is troubling goes like this **************************************************************** #include <istream> #include <ostream> #include <fstream>
5
by: Assertor | last post by:
Hi, all. Is there any way to create an instance of std::ifstream using std::string. (through std::ifstream's constructor or assignment operator or iterator, etc...) i.e. std::string str =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.