Friday, October 31, 2014

Digest for comp.lang.c++@googlegroups.com - 25 updates in 6 topics

comp.lang.c++@googlegroups.com Google Groups
Unsure why you received this message? You previously subscribed to digests from this group, but we haven't been sending them for a while. We fixed that, but if you don't want to get these messages, send an email to comp.lang.c+++unsubscribe@googlegroups.com.
Marcel Mueller <news.5.maazl@spamgourmet.org>: Oct 31 10:43PM +0100

I recently had a problem with a GPU assembler. The program is mainly
C++11, but the parsing of expression components is done with sscanf.
This causes problems because sscanf cannot read the integer constant
"0x80000000". %i returns 0x7fffffff and %u cannot read hex numbers. I
could bet that this has been working with %i for many years.
 
I just tested with gcc 4.8.2 for OS/2 - works with %i.
But with gcc 4.8.2 Linux (Mint 17) the result is the bitwise not.
 
#include <stdio.h>
int main()
{ int i;
sscanf("0x80000000", "%i", &i);
printf("%x", i);
return 0;
}
 
The goal is to read an unsigned number in all common formats (decimal,
binary, octal, hex) from a string. The string does not necessarily end
after the number. I did not find format specifier that does the job.
 
 
Marcel
Melzzzzz <mel@zzzzz.com>: Oct 31 10:54PM +0100

On Fri, 31 Oct 2014 22:43:23 +0100
> necessarily end after the number. I did not find format specifier
> that does the job.
 
> Marcel
 
#include <stdio.h>
 
int main()
{ long long i;
sscanf("0x80000000","%lli",&i);
printf("%llx\n", i);
return 0;
}
 
Problem is that 32 bit int is not long enough, just use long long
and all set...
 
--
Manjaro all the way!
http://manjaro.org/
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 31 09:48AM -0700

On Friday, October 31, 2014 9:10:51 AM UTC-4, David Brown wrote:
> Re-consider it - you don't want the same expression to mean different
> things when it is written alone, or part of another statement.
 
David, what does *tnX++; mean when used by itself?
 
Best regards,
Rick C. Hodgin
Nobody <nobody@nowhere.invalid>: Oct 31 07:26PM

On Fri, 31 Oct 2014 09:48:57 -0700, Rick C. Hodgin wrote:
 
>> Re-consider it - you don't want the same expression to mean different
>> things when it is written alone, or part of another statement.
 
> David, what does *tnX++; mean when used by itself?
 
It means the same as "*(tnX++);".
 
In most cases, that means the same as just "tnX++;", because you're not
actually using the result of the dereference and a dereference doesn't
normally have side effects.
 
But if tnX is "volatile T*", the two aren't the same and the original
statement may actually be useful (e.g. forcing data to be copied from
main memory to cache or from virtual memory to main memory, or triggering
an action which occurs when a memory-mapped hardware register is read from).
 
Yet, such cases may well occur in real-world code (and not (just) because
the programmer meant to write something else). They may result from macro
expansion; e.g. consider the macro
 
#define getch(fp) (*(fp)->ptr++)
 
typically used as "c = getch(fp);" but quite reasonably used as just
"getch(fp);" if you want to skip a character.
 
Such issues (warnings about valid but "odd" constructs) occur frequently
in machine-generated code. Having the compiler attempt to second-guess the
intent in such cases would make it much harder to generate reliable code.
And it would probably also make it harder for humans, who won't always
remember (or even know about) all of the "special case" rules.
 
The problem with "DWIM" ("Do What I Mean") is: who is the "I"? The
person that developed the software, or the person trying to use it?
Richard Damon <Richard@Damon-Family.org>: Oct 31 12:28PM -0700

On 10/31/14, 9:48 AM, Rick C. Hodgin wrote:
 
> David, what does *tnX++; mean when used by itself?
 
> Best regards,
> Rick C. Hodgin
 
Increment tnX, and then provide the value that the previous value of tnX
pointed to (which is then ignored if the phrase is written alone).
 
Something like (where temp has the same type as tnX):
 
( (temp = tnX) , (tnx = tnx+1) , (*tnX))
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 31 02:18PM -0700

On Friday, October 31, 2014 3:25:08 PM UTC-4, Nobody wrote:
> remember (or even know about) all of the "special case" rules.
 
> The problem with "DWIM" ("Do What I Mean") is: who is the "I"? The
> person that developed the software, or the person trying to use it?
 
I appreciate your input. I will consider it.
 
FWIW, I hold that volatile constraints place things in a completely
separate category. And in this case, outside of their use, neither
GCC or MSVC generated assembly opcodes for the dereference in non-
optimization mode (where one would expect it to be generated if any
were to be generated).
 
To me, that non-generation is telling.
 
Best regards,
Rick C. Hodgin
 
PS - Sylvia told me you called today, but then hung up? What's up with
that? :-) (+2 if you get that ancient reference)
legalize+jeeves@mail.xmission.com (Richard): Oct 31 04:44PM

[Please do not mail me a copy of your followup]
 
Wouter van Ooijen <wouter@voti.nl> spake the secret code
 
>Sheet 8 states "new should only appear in constructors". What about an
>assignment that must do a deep copy?
 
I had you on mute because you were feeding trolls on the religious
thread.
 
You shouldn't ever need naked new or naked delete anywhere except
c'tors/d'tors for resource handling classes.
 
This implies that you don't have any raw pointers that indicate
ownership of resources, except in such resource handling classes.
 
If you follow that advice, then deep copy is simply member-wise
assignment and that is by definition the compiler generated copy
c'tor.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
legalize+jeeves@mail.xmission.com (Richard): Oct 31 04:45PM

[Please do not mail me a copy of your followup]
 
Wouter van Ooijen <wouter@voti.nl> spake the secret code
 
>> thus invoking the assignment of objects, not their pointers.
 
>If the pointed-to object is an array of let's say chars that can have a
>different length this won't work very well.
 
Use std::vector<char> and it's fine.
 
You shouldn't have naked pointers to indicate ownership, unless you
are writing a resource managing class like std::vector<>,
std::shared_ptr<>, etc.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
legalize+jeeves@mail.xmission.com (Richard): Oct 31 04:48PM

[Please do not mail me a copy of your followup]
 
Wouter van Ooijen <wouter@voti.nl> spake the secret code
 
>> Once you know the rules, you know when they don't apply.
 
>Not completely true. If you know how it works you know what to do: you
>don't need a rule.
 
It's advice, not a rule. Avoiding this advice will cause yourself
needless pain.
 
Seriously, this advice has been in books by Stroustrup and others, as
well as general advice in newsgroups, for at least 20 years.
 
Whenever I see people whinging about how difficult C++ is and how it's
a source of bugs, it's because THEY DIDN'T FOLLOW THE ADVICE.
 
You can take the advice and make it a rule and you'd be much better
off, but plain and simple Stroustrup doesn't set himself up as a
dictator and neither do I. We offer advice based on the experience of
seeing people over and over again ignore the advice and code themself
up a bucket of snakes. If you want to shoot yourself in the foot, be
my guest.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
woodbrian77@gmail.com: Oct 31 12:29PM -0700

On Friday, October 31, 2014 11:44:37 AM UTC-5, Richard wrote:
> >assignment that must do a deep copy?
 
> You shouldn't ever need naked new or naked delete anywhere except
> c'tors/d'tors for resource handling classes.
 
I get the impression that some here don't like it in even
these cases. I'm fine with it there.
 
I've compared these two lines:
 
auto req=::std::unique_ptr<cmw_request>(::new cmw_request(localbuf));
 
auto req=::std::make_unique<cmw_request>(localbuf);
 
 
On clang 3.3, the make_unique version increases the size
of a text segment by 112 bytes. I think I've tried it
on some newer compilers and found similar results.
 
Brian
Ebenezer Enterprises - Create in me a clean heart, O G-d,
And renew a steadfast spirit within me. Psalms 51:10
 
http://webEbenezer.net
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 31 07:15AM -0700

On Friday, October 31, 2014 9:44:12 AM UTC-4, David Brown wrote:
> He may be talking about many people, including me. I've been declared
> to be someone Rick will not answer, though I actually am not sure why.
 
I hereby reset my "someone Rick will not answer" list to be completely
empty. Everything starts anew! :-)
 
The people who have gone on that list, and others will go on it again I
have no doubts, have been demeaning and hurtful, purposefully negative
rather than honestly helpful, and as a pattern lasting for more than a
few posts.
 
There is a spirit which operates in this world. We cannot see him with
our eyes, just as we cannot see God, but he is there, just as He is there.
 
What we can see is the action in people because of the spiritual presence.
The devil and his demon imps stir up hate, violence, demeaning speech, in
people. He tempts them to lust, commit acts of sin, satiate self at the
expense of others, or of righteousness.
 
I cease speaking with people who are moved by that spirit because that
spirit is there for only one reason: to steal, kill, destroy, whatever it
can (ideas, hope, inspiration, everything). Such a spirit cannot be
reasoned with, talked to, it will only and always turn anything said into
a debate or argument, trying to twist things around into man's reasoning
rather than a solid faith upon/in God.
 
There is a real spiritual battle taking place in this world. It's why
there is so much hate and harm. And Jesus really is the ONLY way out
of that hate because He is the only one who has overcome all of it.
Because of His victory, those who put their faith in Him also stand in
victory because He gives to all who come to Him, without any exceptions,
provided you come to Him as He is, seeking from your inmost heart the
Truth. It is the only way He is found ... when you seek Him with all
your heart.
 
http://biblehub.com/jeremiah/29-13.htm
"13 And ye shall seek me, and find me, when ye shall search for me with
all your heart."
 
People are unaware they are letting Satan and his demon imps work
through them because of the fallen sin nature and man's inability to
know the truth by himself. It is a supernatural act of God that any
man can be restored. But God looks into a person's true self and
knows if a man will hear the Truth, and if so He moves in that person's
life to change the course over time, so that they will be saved.
 
It is a true daily battle for every Christian. And also for every non-
Christian, but the non-Christian does not recognize there is a battle
taking place and is generally left alone because they're already where
Satan wants them (on the path to Hell). But for those who stand up and
speak unto God, and who speak of Jesus Christ and each of our need for
forgiveness by Him through His atoning sacrifice on the cross, that's
where the enemy's fire goes.
 
There's a great sermon about this by Steve Lawson. I would advise
everyone watch it and listen to what he's teaching. He's teaching
that there is an invisible war taking place all around us, that
things are happening in the spiritual realm which translate into
physical action down here upon this Earth in people, in even nations
rising up against nations. The powers that are at work are massive.
In Lawson's teaching he speaks about Job from the Bible who had
everything he possessed taken from him when God gave Satan permission
to lay his hand upon everything he had. And Satan did it because he
hates God. He hates you and me. All he is is hate.
 
Do you know there are only three times in scripture we actually hear
the voice of Satan recorded? Look at the overall theme here (para-
phrased):
 
(1) Garden of Eden, to Adam and Eve. "God's withholding His
blessing by keeping you from the fruit of the tree of the
knowledge of good and evil."
 
(2) In Heaven to God, regarding Job. "God, you're being too
good to Job. If you took away his stuff, he'll curse you
to your face."
 
(3) In the wilderness to Jesus during Jesus' 40-day fast.
"All of this has been given to me, and I can give it to
whomever I choose. If you will bow down and worship me,
I'll be better to you than God."
 
He tells Adam and Eve "God's being too good to you," then God, "You're
being too good to them," and then Jesus, "I'll be better to you than
God."
 
He is only and always setting everyone against everyone else because
he wants to be God and he hates the fact that he cannot be God because
there is only one God who is God. He is God the Father, God the Son,
and God the Holy Spirit ... the Almighty.
 
A description of Satan's goals:
http://biblehub.com/isaiah/14-13.htm
http://biblehub.com/isaiah/14-14.htm
http://biblehub.com/isaiah/14-15.htm
 
"13 For thou hast said in thine heart, I will ascend into heaven, I
will exalt my throne above the stars of God: I will sit also upon the
mount of the congregation, in the sides of the north."
 
"14 I will ascend above the heights of the clouds; I will be like the
most High."
 
"15 Yet thou shalt be brought down to hell, to the sides of the pit."
 
That is the enemy we face. One who desires to rule the Heavens and
be God. And there is not one of us upon this Earth who can stand
against him, but only the Lord Jesus Christ, rightly called "Lord"
because of what He's done, and because of who He is.
 
Make no mistake about it ... we are all in this war. Some will wind
up on the losing side because they would not humble themselves.
Others will forgo self and submit themselves unto the Lord humbly,
and ask forgiveness, and be forgiveness, and shine like the stars
in Heaven forever.
 
Each man has a choice to make. And the enemy is working continually
in your life to stop you from making the right one. But all you have
to do is turn to Him in your heart, in the quiet recesses of your mind,
and ask Him to save you. Ask Him to come to you and lead you out of
captivity and He will do so. He hears your thoughts. He knows your
heart. He knows your truest intentions and aspirations. When you
humble yourself and come to Him, He will ALREADY have been working on
your life to save you, because He knows your thoughts afar off.
 
Come to Him, and ask forgiveness, and ask Him to save you, and you
too will be saved this very day. You'll be forever glad you did.
 
Best regards,
Rick C. Hodgin
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 31 07:20AM -0700

The Invisible War, by Steve Lawson:
http://www.libsf.org/video/steve_lawson__the_invisible_war.mp4
 
You can also find it on YouTube by googling:
"YouTube Steve Lawson The Invisible War"
 
Here is the full video on YouTube:
https://www.youtube.com/watch?v=UDVWvOQvlK8
 
Best regards,
Rick C. Hodgin
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Oct 31 02:59PM

On 31/10/2014 13:40, Rick C. Hodgin wrote:
> use only underscores or some type of case like camelCase.
> However, if you want to use spaces, then it's now available (or
> soon will be, James 4:15).
 
James was a cunt but not as much of a cunt as Jesus; or to put it
another way: stop fucking preaching in this technical newsgroup.
 
 
> (2) No one is forced to use RDC. People will use it because they
> want to, and for no other reason. There are lots of other compilers
> with all of their favorite stand-bys.
 
I suspect no one will use this "RDC" thing except you if this spaces
debacle is indicative of how you approach things
 
 
> (3) A person could easily parse and type in the ASCII-255 character. In
> Windows it's Alt+numpad 255. I'm sure it's possible in Linux too.
 
Yeah, twice as many keystrokes as underscore and four times as many as
multiple underscores.
 
> the display to in some way mark off the single variable with the
> nbsp character ... then perhaps you should switch to an open source
> editor? Just a thought...
 
Yeah switch to an open source editor just so they can type spaces that
aren't spaces in things that shouldn't have spaces.
 
/Flibble
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Oct 31 03:03PM

On 31/10/2014 14:15, Rick C. Hodgin wrote:
> your life to save you, because He knows your thoughts afar off.
 
> Come to Him, and ask forgiveness, and ask Him to save you, and you
> too will be saved this very day. You'll be forever glad you did.
 
 
Mate, just fuck off.
 
/Flibble
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 31 08:15AM -0700

Mr Flibble wrote:
> [snip]
 
Mr Flibble, I love you. Jesus loves you more.
 
As He brings you to my remembrance, I will keep you in my prayers.
Take care my friend. I will be here if you would like to contact
me. And remember, there are none so far away from the Lord that
His tremendous Love cannot reach, should they have even the smallest
inkling of desire from within to seek Him. He is able to save to
the uttermost.
 
May He guide you from within, Mr Flibble, forever to His Kingdom
in the glories of Heaven.
 
Best regards,
Rick C. Hodgin
Robert Wessel <robertwessel2@yahoo.com>: Oct 31 10:35AM -0500

On Fri, 31 Oct 2014 06:40:25 -0700 (PDT), "Rick C. Hodgin"
> use only underscores or some type of case like camelCase.
> However, if you want to use spaces, then it's now available (or
> soon will be, James 4:15).
 
 
While *I* might avoid the embedded pseudo-spaces, but if I have to
look at source code that someone else has written, all my tools are
trashed.
 
 
>(2) No one is forced to use RDC. People will use it because they
> want to, and for no other reason. There are lots of other compilers
> with all of their favorite stand-bys.
 
 
Whatever your goals are, setting up extra roadblocks to the use of
your language seems like a bad idea. Especially when they accomplish
so little. What, again, is so horrible about underscores?
 
The last is a rhetorical question, I've seen you prior
justification(s), which I find wholly unsatisfying, so unless you have
a new one, let's leave it at you really don't like underscores for
some unfathomable (to me) reason.
 
In any event programming languages are not the same as human
languages, and meet different goals. Trying to make a language more
like English leads you down the road of Cobol. Which, despite a
rather convoluted and complex syntax, and a huge effort to actually be
English-like, pretty much failed utterly at that goal.
 
 
>(3) A person could easily parse and type in the ASCII-255 character. In
> Windows it's Alt+numpad 255. I'm sure it's possible in Linux too.
 
 
When you do that, by default, you end up with an 0xa0 in your file. Or
it may depend on which version of notepad you're running. It also
doesn't display well.
 
I expect that in a lot of cases you're going to end up with either a
0xc2 0xa0 or a 0xc3 0xaf in your file, which will probably break
*your* tools.
 
 
> the display to in some way mark off the single variable with the
> nbsp character ... then perhaps you should switch to an open source
> editor? Just a thought...
 
 
It's not just editors, there are a lot of tools which won't deal with
this well, especially once you add the PC/OEM code page vs. Unicode
distinction to the mix.
David Brown <david.brown@hesbynett.no>: Oct 31 05:05PM +0100

On 31/10/14 15:15, Rick C. Hodgin wrote:
> have no doubts, have been demeaning and hurtful, purposefully negative
> rather than honestly helpful, and as a pattern lasting for more than a
> few posts.
 
I call a spade a spade. If I see you have an idea I think is good, I
will say so. If you have an idea that I think can be improved, I will
give suggestions. And if I see an idea that I think is utterly stupid,
I will call it utterly stupid. If you find that "demeaning", then I
suggest you think through some of your ideas a little better - and
listen to the responses and advice given. My aim in the technical
comments I post is to be helpful - if I have been cruel, it is to be kind.
 
(In case it is still not entirely clear, I think many of your ideas are
good or at least interesting, and while they may not make sense for C
they would be fine for your own language. But a few of them, such as
your weird spaces idea, are /really/ bad, and it would be disrespectful
to you if people let them pass without trying to persuade you to drop them.)
 
Note also that I am condemning the "sin", not the "sinner".
 
 
Regarding your religious posts and comments, it's been made abundantly
clear that they are not welcome here. Nobody - Christians, atheists,
agnostics, pastafarians, or whatever - appreciates them. Nobody thinks
they are of any benefit or help. Even the other Christians in this
group understand that the way you write is going to chase people away
rather than persuade people to consider Christianity. In your own
terms, /you/ are doing Satan's work.
 
Unlike many here, I am quite happy with the occasional religious thread,
just as I am quite happy with the occasional political or other
off-topic thread. But it should be kept in a separate thread. There is
a time and a place for such discussions - I am sure you know the
Biblical quotation about there being a time for all things. And when
there /is/ a religious discussion, you will get on far better by keeping
to the point and the issues at hand, rather than continuously re-cycling
the same theories about a "war". If you like, try /fighting/ the "war"
by being a positive influence in your posts, rather than just moaning
about it.
 
Again, if you find this hurtful then I am sorry for you - but I write
this to be helpful to /you/.
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 31 09:33AM -0700

On Friday, October 31, 2014 12:05:30 PM UTC-4, David Brown wrote:
> Regarding your religious posts and comments, it's been made abundantly
> clear that they are not welcome here.
 
The Lord will not ask me on the final day if I was popular, or if I
made converts, but only if I obeyed Him. Have you ever read Matthew
28:18+? And John 13:34 (or printf("John \015:\042\n"); // If you prefer)?
There, I used octal. :-)
 
http://biblehub.com/matthew/28-18.htm // All authority, so I tell you:
http://biblehub.com/matthew/28-19.htm // Go, teach, even baptize
http://biblehub.com/matthew/28-20.htm // Teach them to obey me
http://biblehub.com/matthew/28-21.htm // I am with you always
 
http://biblehub.com/john/13-34.htm // Love one another
// "as I have loved you"
 
Best regards,
Rick C. Hodgin
Richard Damon <Richard@Damon-Family.org>: Oct 31 12:04PM -0700

On 10/29/14, 11:31 AM, Rick C. Hodgin wrote:
> code 255. It looks just like a space even. :-)
 
> Best regards,
> Rick C. Hodgin
 
One comment about use '255' as the value of your 'space' character, is
that seems to be implying that your language has, in effect, defined
what 'code page' you system needs to be in. In effect, this limits what
human languages are expressible in the file, including comments and
strings.
 
It would seem much better to adopt the Unicode encoding for your
character set. You might still allow some way to define that a given
file is written in a extended ASCII 8 bit character set (and defining
WHICH extended 8 bit character set it is in).
 
If you do this, then the value of the non-break-space character is
U+00A0 (how it will appear in the file depends on what encoding you
choose (UTF-8 may be the best, which would give you 0xC2, 0xA0 as a
multi-byte character).
drew@furrfu.invalid (Drew Lawson): Oct 31 07:11PM

In article <228d9393-add0-4911-805c-99db8671e9df@googlegroups.com>
 
>28:18+? And John 13:34 (or printf("John \015:\042\n"); // If you prefer)?
 
That prints:
:"hn
 
(And this from someone claiming to know when character substitutions make sense.)
 
--
Drew Lawson | Pass the tea and sympathy
| for he good old days are dead
| Let's raise a toast to those
| who best survived the life they led
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Oct 31 12:14PM -0700

On Friday, October 31, 2014 3:11:14 PM UTC-4, Drew Lawson wrote:
> :"hn
 
> (And this from someone claiming to know when character substitutions
> make sense.)
 
:-)
 
Best regards
Rick C. Hodgin
porparek@gmail.com: Oct 31 10:03AM -0700

Hi,
 
I found that returning from my_fun is costless. None of the constructors or assignment operator of Human is called.
I expect the non-default constructor of Human to be called as the result of "return value optimization".
Why is that ?
 
I compiled it as follows (no c++11):
g++ -O0 my_prog.cpp
 
I use gcc 4.9.1 and boost 1.56.0.
 
#include <iostream>
#include <boost/tuple/tuple.hpp>
 
using namespace boost;
using namespace std;
 
class Human
{
public:
string m_name;
 
Human(void)
{
cout << "Human default constructor" << endl;
}

Human(string name) : m_name(name)
{
cout << "Human non-default constructor" << endl;
}

Human(const Human &b) : m_name(b.m_name)
{
cout << "Human copy constructor" << endl;
}
 
Human &operator=(const Human &right)
{
cout << "Human operator=" << endl;
m_name = right.m_name;
return *this;
}
};
 
boost::tuple<Human, string> my_fun(void)
{
Human h("xx"); // constructor
string pet("zz");
return boost::make_tuple(h, pet); // make_tuple calls copy constructor
}
 
int main(void)
{
boost::tuple<Human, string> myTuple = my_fun (); // nothing is called (excluding the constructor called inside my_fun and copy constructor called by make_tuple). Does this mean that return operation is costless ? How can it be ?
return 0;
}
 
thanks for clarification
Victor Bazarov <v.bazarov@comcast.invalid>: Oct 31 01:35PM -0400

> I found that returning from my_fun is costless. None of the constructors or assignment operator of Human is called.
> I expect the non-default constructor of Human to be called as the result of "return value optimization".
> Why is that ?
 
Why is what? Why is it that you expect the non-default c-tor of Human
to be called? I don't know. Why do you?
 
RVO allows the compiler to eliminate copying from the value in the
'return' expression in some function whose body the compiler can see,
when that value is used to initialize another object of the same type.
 
blah func_returning_a_blah()
{
...
return some_blah;
}
...
blah b = func_returning_a_blah(); // no copying due to RVO
 
If the compiler can see the function, it will inline the code in such a
way so it initialized the 'some_blah' directly into 'b'.
 
 
> int main(void)
> {
> boost::tuple<Human, string> myTuple = my_fun (); // nothing is called (excluding the constructor called inside my_fun and copy constructor called by make_tuple). Does this mean that return operation is costless ? How can it be ?
 
Costless, yes. What's the big deal?
 
As an experiment, move 'my_fun' function definition to a separate
translation unit, and you *might* see a copy made, depending on how
clever your compiler is. However, often the compiler can create a
hidden "argument" for a value-returning function for the return value
(since it is needed by the caller anyway), and in the case of the
initialization, the address of the object being initialized is going to
be used as that hidden "argument", so even after moving the function so
it can't be inlined any longer, you are still going to see no copy made.
 
> return 0;
> }
 
> thanks for clarification
 
V
--
I do not respond to top-posted replies, please don't ask
Paavo Helde <myfirstname@osa.pri.ee>: Oct 31 01:46PM -0500

porparek@gmail.com wrote in
 
> "return value optimization"
> How can it be ?
 
I have understood that typically the caller reserves some space on the
stack for the result variable and passes its address to the called
function. When the function comes around to construct the return value or a
named variable which will be returned later, it constructs it directly at
the specified address. This requires some cooperation between the caller
and the called function. For details see
 
http://en.wikipedia.org/wiki/Return_value_optimization
legalize+jeeves@mail.xmission.com (Richard): Oct 31 04:38PM

[Please do not mail me a copy of your followup]
 
On November 12th, the Utah C++ Programmers group will have their next
meetup. We'll discuss how to make efficient recursive descent parsers
using the Boost.Spirit library. We will also be raffling off a Visual
Sutdio Ultimate with MSDN Universal subscription. This is a $13,300
value courtesy of Microsoft.
 
Details here:
<http://www.meetup.com/Utah-Cpp-Programmers/events/210984212/>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: