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::istre am_iterator<cha r>(file),
(std::istream_i terator<char>() )); // extra brackets here
}
} 4 3830
On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwro te:
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::istre am_iterator<cha r>(file),
(std::istream_i terator<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_iterato r<charas first argument, a pointer to
a function returning an istream_iterato r<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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze schrieb:
On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwro te:
>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::istre am_iterator<cha r>(file), (std::istream_i terator<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_iterato r<charas first argument, a pointer to
a function returning an istream_iterato r<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 objektorientier ter Datenverarbeitu ng
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
On Dec 16, 12:01 pm, Chris Forone <4...@gmx.atwro te:
James Kanze schrieb:
On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwro te:
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::istre am_iterator<cha r>(file),
(std::istream_i terator<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_iterato r<charas first argument, a pointer to
a function returning an istream_iterato r<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_it erator<char>(fi le)
it is a declaration (of a variable named file, with type
std::istream_it erator< char >), unless the context where it
appears doesn't allow declarations.
The case of:
std::istream_it erator<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 "reinterpre ted" 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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
James Kanze schrieb:
On Dec 16, 12:01 pm, Chris Forone <4...@gmx.atwro te:
>James Kanze schrieb:
>>On Dec 16, 10:03 am, Chris Forone <4...@gmx.atwro te: 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::istre am_iterator<cha r>(file), (std::istream_i terator<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_iterato r<charas first argument, a pointer to a function returning an istream_iterato r<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_it erator<char>(fi le)
it is a declaration (of a variable named file, with type
std::istream_it erator< char >), unless the context where it
appears doesn't allow declarations.
The case of:
std::istream_it erator<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 "reinterpre ted" 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 objektorientier ter Datenverarbeitu ng
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the easiest way
to take a huge project across onto it.
Basically, I've hit a problem. I have no idea how the ifstream class
handles directories. In the code I have (which I didn't write), there
are several places whereby an ifstream stream is...
|
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"
file_names = "file2.txt"
etc ...
|
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"
10,20,30
|
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 function called readline() because I
would also like to do some processing on the line each time (ignoring
comments and so on).
I have:
|
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
| |
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 in("in.txt"); if(!in) {cout << "error\n";}
in.close(); if(!in) {cout << "error\n";}
in.close(); if(!in) {cout << "error\n";}
|
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");
|
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>
|
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 = "this is a string";
std::ifstream ifs = str; //<----- this is a just pseudo code.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |