Saturday, June 20, 2015

Digest for comp.lang.c++@googlegroups.com - 12 updates in 4 topics

JiiPee <no@notvalid.com>: Jun 20 11:54AM +0100

I pretty much understand const correctness. But there is one thing I do
not understand 100%:
 
class A
{
public:
int& foo() const // does not compile, should be: const int& foo() const
{
return p_;
}
private:
int p_;
};
 
This does not compile because there foo should return a const reference:
const int& foo() const
 
But why? Because to my understanding a const function ( "...foo() const"
) means that the *function foo* promises not to change the data members
of the object. If I return a reference to a member then foo is not
changing the data members, right? Yes, the caller can change the private
member later on, but foo did not. What am I not understanding correctly?
Or is it that the definition of const function: "the function is not
changing the state of the object" is not accurate but should be like
"the function is not changing the state of the object AND also the
caller cannot change the state of the object with the return value"?
 
What is the real and full definition of "const member function"?
Obviously it is not "the *function* promises not to change the data
members of the object". It is confusing because it seems like most of
the web sites seem to define it wrongly like that.
Victor Bazarov <v.bazarov@comcast.invalid>: Jun 20 07:41AM -0400

On 6/20/2015 6:54 AM, JiiPee wrote:
> Obviously it is not "the *function* promises not to change the data
> members of the object". It is confusing because it seems like most of
> the web sites seem to define it wrongly like that.
 
It has nothing to do with what 'foo' does to the object. It is a side
effect of the fact that '*this' is constant *inside* that function.
Every part of '*this' is considered constant *inside* 'foo'. When you
try to return 'p_', it is constant, and the compiler would have to
convert a const lvalue to a non-const lvalue in order to bind the
reference (the return value) to it. *That* is prohibited.
 
You're free to return a non-const ref to anything else, for instance to
a static object defined inside 'foo'.
 
V
--
I do not respond to top-posted replies, please don't ask
JiiPee <no@notvalid.com>: Jun 20 01:23PM +0100

On 20/06/2015 12:41, Victor Bazarov wrote:
>> members of the object". It is confusing because it seems like most of
>> the web sites seem to define it wrongly like that.
 
> It has nothing to do with what 'foo' does to the object.
 
just to clarify,
"it" - are you refering here to the definition of the "const member
function"?
 
> 'foo'. When you try to return 'p_', it is constant, and the compiler
> would have to convert a const lvalue to a non-const lvalue in order to
> bind the reference (the return value) to it. *That* is prohibited.
 
ok, so we can think that p_ (after declaring the function as const
function) becomes like:
const int p_;
And then for example for:
int foo() const { return p_; }
it would mean that we are doing a copy for the return value:
int return = p_;
which works because const can be copied to an int.
But with reference return value it would be like:
int& return = p_;
which is not working because non-const ref cannot refer to a const. Right?
 
If I see it like this then its easy to understand....
JiiPee <no@notvalid.com>: Jun 20 01:24PM +0100

On 20/06/2015 13:23, JiiPee wrote:
> int foo() const { return p_; }
> it would mean that we are doing a copy for the return value:
 
> int return = p_;
 
well, better would be "returnValue = ..." not to confuse with the
keyword return, but am sure you got it :)
gwowen <gwowen@gmail.com>: Jun 19 11:16PM -0700

On Friday, June 19, 2015 at 11:45:46 PM UTC+1, bartekltg wrote:
 
> This may be measurement error, but it looks like my
> recursion solution is 30% faster than yours (on my
> computer, complicator etc.)
 
Well, I do build more than 6,500 totally unnecessary strings in an incredibly inefficient way. That's going to dominate the runtime.
 
> Other things than recursion vs. loops were more important
> (for example, complexity: O(3^n) instead of O(n*3^n))
 
I'd be interested to see how you got O(3^n) for the recursive version. Not questioning it, just interested.
gwowen <gwowen@gmail.com>: Jun 19 11:26PM -0700

On Saturday, June 20, 2015 at 7:16:50 AM UTC+1, gwowen wrote:
> I'd be interested to see how you got O(3^n) for the recursive version.
> Not questioning it, just interested.
 
No need Bart, I thought about it and I see now.
That's definitely a win for recursion when n gets large (as long as you can avoid smashing the stack). Tail-recursion would help.
bartekltg <bartekltg@gmail.com>: Jun 20 02:21PM +0200

On 20.06.2015 08:26, gwowen wrote:
> On Saturday, June 20, 2015 at 7:16:50 AM UTC+1, gwowen wrote:
>> I'd be interested to see how you got O(3^n) for the recursive version.
>> Not questioning it, just interested.
 
This post was superseded or my server lost it?
 
I have posted the solution in this thread earlier:
Message-ID: <mm0u43$7ie$1@node1.news.atman.pl>
Not the prettiest one, but I think quite readable.
 
> No need Bart, I thought about it and I see now. That's definitely a
> win for recursion when n gets large (as long as you can avoid
> smashing the stack). Tail-recursion would help.
 
In this particular problem when a stack is to short,
life of the universe is to short to finish the computation too ;-)
 
I have no idea how good gcc is with tail-recursion (gcc is not ocaml;-)
In my code t-r is not used (A looked at asm output, three recursive
calls, and with tail-recursion should be two). But simple factorial
code gcc compile correctly to loop.
 
int factorial (int x){
if (x>1) return x*factorial(x-1);
else return x;
}
 
.type _Z9factoriali, @function
_Z9factoriali:
.LFB1879:
.cfi_startproc
cmpl $1, %edi
jle .L50
movl $1, %edx
.p2align 4,,10
.p2align 3
.L49:
leal -1(%rdi), %eax
imull %edi, %edx
cmpl $1, %eax
movl %eax, %edi
jne .L49
.L48:
imull %edx, %eax
ret
.L50:
movl %edi, %eax
movl $1, %edx
jmp .L48
 
 
bartekltg
Noob <root@127.0.0.1>: Jun 20 10:36AM +0200

On 19/06/2015 21:26, Wouter van Ooijen wrote:
> make me feel that I am missing a compiler feature, it seems to be an
> issue about what constitutes a valid C++ program.
 
> https://gcc.gnu.org/projects/cxx1y.html seems to claim full C++ 14 support.
 
You're right. My comment was based on obsolete info.
 
gcc 4.8 docs stated "Support for C++11 is still experimental, and may
change in incompatible ways in future releases." but that qualification
was removed in gcc 4.9 docs. So I should upgrade my tool chain.
 
But the problem is that this project's Windows binaries are supposed
to be generated with VS, which is lagging in C++11 support.
(Perhaps Microsoft is too busy pushing C#)
 
Regards.
JiiPee <no@notvalid.com>: Jun 20 11:59AM +0100

On 13/06/2015 00:56, Victor Bazarov wrote:
>> »-« operator in their Google search specifications.
 
> Pfft! Real programmers never read manuals! ;-)
 
> V
 
More commonly: Men do not read manuals when buying something and start
using it
Rosario19 <Ros@invalid.invalid>: Jun 20 07:50AM +0200

On Sat, 20 Jun 2015 00:41:07 +0200, David Brown wrote:
 
>seldom smaller than a few nH. You might use pH for the inductance of
>wires, chip pins/balls, pcb vias, etc.
 
>Usually it's clear in the context what you mean by pH.
 
PH whould be a misure for acid-base
gwowen <gwowen@gmail.com>: Jun 19 11:22PM -0700

On Saturday, June 20, 2015 at 6:51:08 AM UTC+1, Rosario19 wrote:
> >Usually it's clear in the context what you mean by pH.
 
> PH whould be a misure for acid-base
 
Quite right. I'd hate to walk all the way to a "PH" on my Ordnance Survey map and discover some sort of chemical works.
 
(For the non-Brits: http://www.ordnancesurvey.co.uk/resources/maps-and-geographic-resources/map-abbreviations.html#p)
Rosario19 <Ros@invalid.invalid>: Jun 20 09:08AM +0200

On Sat, 20 Jun 2015 07:50:53 +0200, Rosario19 wrote:
 
 
>PH whould be a misure for acid-base
 
en.wikipedia.org/?title=PH
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: