 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
NewToTemplates Guest
|
Posted: Fri Jun 24, 2005 7:18 am Post subject: what is "validate" |
|
|
I was told that there is difference between "list++" and "++list". What
is the difference between these two?
list;
what does the above statement do? I was told that it validates the
object. What does it mean?
is validate used only in Templates?
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ferdi Smit Guest
|
Posted: Fri Jun 24, 2005 11:25 am Post subject: Re: what is "validate" |
|
|
"NewToTemplates" <hetzme (AT) yahoo (DOT) com> wrote
| Quote: | I was told that there is difference between "list++" and "++list". What
is the difference between these two?
list;
what does the above statement do? I was told that it validates the
object. What does it mean?
is validate used only in Templates?
|
I think you mean list::iterator. i++ is postifx increment, en ++i is prefix
increment. The difference? i++ will first fetch i, and then increment it;
++i will first increment i and then fetch it. Consider the following:
f(int x) {cout << x;}
int i=0;
f(i++); // outputs 0, i=1
f(++i); // outputs 2, i=2
I don't know what you mean by validate? I think you must mean invalidate,
and then in a different context... A deque erasure for example invalidates
all iterators...so you can't do this: (this goes for vector and list etc as
well)
std::deque
// fill d
for (std::deque<int>::iterator i = d.begin(); i != d.end(); ++i) { // prefix
++i is faster, because it doesn't need to copy i
if (needs_remove(*i)) d.erase(i); // BAD, can't increment i anymore after
the erasure, as it is invalidated.
}
--
Regards,
Ferdi Smit
smit xs4all nl
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Jun 24, 2005 12:39 pm Post subject: Re: what is "validate" |
|
|
NewToTemplates wrote:
| Quote: | I was told that there is difference between "list++" and
"++list". What is the difference between these two?
|
Presuming that by list, you really mean an iterator into a list,
the difference is the results of the expression: list++ results
in the value before the incrementation, and ++list the value
after.
If you don't use the results, there's really no difference.
| Quote: | list;
what does the above statement do?
|
Absolutely nothing.
| Quote: | I was told that it validates the object. What does it mean?
|
No idea. You'll have to ask whoever told you, because I've
never heard the term applied to a language construct.
(Obviously, you validate all input, and you validate parameters
to functions, and a lot of other things. But, at least in C++,
it always refers to code you have to write.)
| Quote: | is validate used only in Templates?
|
Again, in what sense. I've never heard the term used with
regards to templates.
--
James Kanze GABI Software
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Mark Van Peteghem Guest
|
Posted: Mon Jun 27, 2005 10:37 am Post subject: Re: what is "validate" |
|
|
NewToTemplates wrote:
| Quote: | I was told that there is difference between "list++" and "++list". What
is the difference between these two?
list;
what does the above statement do? I was told that it validates the
object. What does it mean?
I suppose you're using a chained list (in the pre-STL way). You then |
have a class like
class ListNode
{
public:
ListNode(const Data &theData): data(theData), next(0)
{}
Data data;
ListNode *next;
};
A list is then simply an object of type ListNode, that has the first
element of the list, and a pointer to the second element, which has
a pointer to the third, etc. It ends with a ListNode object in which
next is NULL. You can traverse such a list with
for (ListNode *list=myList; list; ++list)
{
... // use list
}
The for-loop ends when list; evaluates to NULL, which means it's at
the end of the loop.
These days it's better to use std::list.
| Quote: | is validate used only in Templates?
No, it can be used in non-template lists, as in the example above, |
but most likely it will be used in templates.
--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
L.Suresh Guest
|
Posted: Tue Jun 28, 2005 12:06 pm Post subject: Re: what is "validate" |
|
|
the post increment creates a temporary and it should be done only if it
is needed..
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | NewToTemplates wrote:
I was told that there is difference between "list++" and
"++list". What is the difference between these two?
Presuming that by list, you really mean an iterator into a list,
the difference is the results of the expression: list++ results
in the value before the incrementation, and ++list the value
after.
If you don't use the results, there's really no difference.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Jun 29, 2005 11:20 am Post subject: Re: what is "validate" |
|
|
L.Suresh wrote:
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote:
NewToTemplates wrote:
I was told that there is difference between "list++" and
"++list". What is the difference between these two?
Presuming that by list, you really mean an iterator into a
list, the difference is the results of the expression:
list++ results in the value before the incrementation, and
++list the value after.
If you don't use the results, there's really no difference.
the post increment creates a temporary and it should be done
only if it is needed..
|
The creation of the temporary or not should have no observable
effect on the program, so who cares. About the only difference
it could possibly make (unless you write very weird ++
operators) is in performance, but when I actually benchmarked,
there was no measurable difference there either.
If you're starting a project from scratch, with no existing
code, you can choose one or the other -- you might as well
choose prefix, since that seems to be the in thing today. Most
of the time, however, the correct form to use is simply the one
that is in use in the rest of the code. Since it doesn't make a
difference, be consistent.
--
James Kanze GABI Software
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
NewToTemplates Guest
|
Posted: Sun Jul 24, 2005 6:06 pm Post subject: Re: what is "validate" |
|
|
Thanks for all the replies. I lost this thread, so could not check for
a while. I am going to summarize everything that I understood from all
the above postings and also add more comments. Please let me know if I
am on the same page with u all.
---------------------------------------------------------------------------------
Ferdi Smit said
"i++ will first fetch i, and then increment it;
++i will first increment i and then fetch it."
How does the compiler interpret the following code?
i++;
++i;
Is the following interpretation correct?
i++;
-----
i;
i = i + 1;
i;
++i;
-----
i = i + 1;
i;
---------------------------------------------------------------------------------
L.Suresh said "the post increment creates a temporary and it should be
done only if it is needed.. "
Suresh, does it call the constructor when it creates the temporary?
---------------------------------------------------------------------------------
James Kanze said "the only difference it could possibly make is in
performance .. "
James, did u mean that in the post-increment operation we execute more
instructions than pre-increment?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Jul 25, 2005 1:22 pm Post subject: Re: what is "validate" |
|
|
NewToTemplates wrote:
| Quote: | ---------------------------------------------------------------------------------
Ferdi Smit said
"i++ will first fetch i, and then increment it;
++i will first increment i and then fetch it."
How does the compiler interpret the following code?
i++;
++i;
Is the following interpretation correct?
i++;
-----
i;
i = i + 1;
i;
|
Rather:
tmp = i ;
i = i + 1 ;
tmp ;
| Quote: | ++i;
-----
i = i + 1;
i;
---------------------------------------------------------------------------------
L.Suresh said "the post increment creates a temporary and it
should be done only if it is needed.. "
Suresh, does it call the constructor when it creates the
temporary?
|
If the type in question has a constructor.
Of course, typically, the compiler is able to see through all
this, and will generate the same code, or very similar code,
regardless of the form.
| Quote: | ---------------------------------------------------------------------------------
James Kanze said "the only difference it could possibly make
is in performance .. "
|
Somethings missing in the context there. (I was responding to a
specific claim, not making a general statement.)
The results of ++ i and i ++ are different. If you use the
results, it very much makes a difference. And which one you
choose depends on which results you want to use.
If you don't use the results, the only possible difference it
could possibly make is in performance. And even that should be
qualified with something along the lines of "supposing
reasonable definitions of the operator." A user defined
operator can do just about anything, and a user could,
plausibly, define prefix to read from a data base, and postfix
to write to it. I (and I suspect most others) would not
consider that a reasonable use of ++.
| Quote: | James, did u mean that in the post-increment operation we
execute more instructions than pre-increment?
|
On a built-in type, I doubt that there is a compiler in the
world which would generate different code, even without
optimization, and supposing that the results were not used. For
a user defined type, the "classic" implementation of post-fix
requires constructing an additional variable. If the operator
is simple and inlined, compilers seem to be able to detect that
this variable is not used, and suppress it. If the compiler
doesn't suppress it, however, then the post-increment form will
pay the cost of creating the additional variable.
In the past, some very emminant experts have suggested that
because of this, you should always use prefix if you don't use
the results. My own benchmarks show that in fact, the
difference isn't measurable, so I would recommend a much watered
down form of the rule: if you are starting a new project, with
no existing code, use prefix by default, because it will never
be slower. If, however, you have existing code, and it is
consistant, continue doing whatever it does, because the
difference in performance almost certainly won't be measurable,
so the change isn't worth the bother. (Of course, if in your
code, it turns out that the difference is measurable, you then
have to weigh the cost of changine all of the existing code
against that of being inconsistant. If the inconsistency is
limited to one critical loop, it can easily be handled with a
comment, but if it is wider spread, the best choice may not be
obvious.)
--
James Kanze GABI Software
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|