Wednesday, July 8, 2015

Digest for comp.lang.c++@googlegroups.com - 7 updates in 2 topics

Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Jul 07 11:54PM -0600


>Is ref an alias for vec[5] or for vec[index]? In other words if index changes from 5 to 6, does ref now refer to vec[6]? Or does ref always refer to vec[5]?
 
>Of course, I could just test it, but I would think there's a simple way of thinking about it that makes the answer apparent and unambiguous.
 
>It's clear to me that if we write int& i = index; int& clearRef = vec[i]; then surely clearRef always refers to vec[index] however index changes. But the previous case is unclear to me.
 
With respect to your last example, I'm not sure exactly what you mean
by "vec[index] however index changes." Does your understanding match
the result of running the following?
 
Code:
==============
#include <iostream>
 
int main()
{
int index = 0;
int& i = index;
int vec[] = {1, 2};
int& clearRef = vec[i];
 
std::cout << clearRef << "\n";
 
index = 1;
std::cout << clearRef << "\n";
 
return 0;
}
==============
 
Result:
==============
1
1
==============
 
Louis
Paul <pepstein5@gmail.com>: Jul 07 11:50PM -0700

On Wednesday, July 8, 2015 at 6:55:16 AM UTC+1, Louis Krupp wrote:
> 1
> 1
> ==============
 
Thanks, Louis. No, this doesn't match my understanding. I should do more testing, rather than just assuming things. I don't understand this result at all. I would have thought vec[i] means "take the nth component of vec where n is the integer to which i refers". Hence I expect vec[i] to change from vec[0] to vec[1] whenever index changes from 0 to 1. Why does this not happen? I suppose that, if I do want this behaviour, the best technique is via lambda functions. Is that correct?
 
Thank you,
 
Paul
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Jul 08 01:19AM -0600

On Tue, 7 Jul 2015 23:50:10 -0700 (PDT), Paul <pepstein5@gmail.com>
wrote:
 
>> 1
>> ==============
 
>Thanks, Louis. No, this doesn't match my understanding. I should do more testing, rather than just assuming things. I don't understand this result at all. I would have thought vec[i] means "take the nth component of vec where n is the integer to which i refers". Hence I expect vec[i] to change from vec[0] to vec[1] whenever index changes from 0 to 1. Why does this not happen? I suppose that, if I do want this behaviour, the best technique is via lambda functions. Is that correct?
 
Beats me. I've never used lambda functions.
 
As I understand it, when you declare clearRef like this:
 
int& i = index;
int& clearRef = vec[i];
 
clearRef becomes an alias for the element of vec specified by the
value of i at the time clearRef is declared. Since i is an alias for
index, it's the same as saying:
 
int& clearRef = vec[indexi];
 
If index is 0 when clearRef is declared, clearRef becomes an alias for
vec[0], and subsequent changes to index or i don't change clearRef.
 
The following might help:
 
Code:
==============
#include <iostream>
 
int main()
{
int index = 0;
int& i = index;
int vec[] = {1, 2};
int& clearRef = vec[i];
 
std::cout << vec[i] << "\n";
std::cout << clearRef << "\n";
 
index = 1;
std::cout << vec[i] << "\n";
std::cout << clearRef << "\n";
 
return 0;
}
==============
 
Result:
==============
1
1
2
1
==============
 
Louis
Rosario19 <Ros@invalid.invalid>: Jul 08 09:22AM +0200


>However, this is ambiguous (to me). Take the following case.
>std::vector<int> vec; /* ..... */ int index = 5; int& ref = vec[index];
 
>Is ref an alias for vec[5] or for vec[index]?
for me
 
int &i=y;
 
means &i=&y;
 
so i is what contain &y
 
for me &ref is &(vec[5]) or vec+5 so ref=vect[5]
 
i don't know if i make one error
 
"Öö Tiib" <ootiib@hot.ee>: Jul 08 12:36AM -0700

On Wednesday, 8 July 2015 09:50:23 UTC+3, Paul wrote:
> do more testing, rather than just assuming things. I don't understand
> this result at all. I would have thought vec[i] means "take the nth
> component of vec where n is the integer to which i refers".
 
It feels that you expect reference to be alias of expression. That
is not true, reference is always reference to concrete object.
On case of 'int&' it becomes reference to some int when defined
and stays like that forever.
 
> Hence I expect vec[i] to change from vec[0] to vec[1] whenever index
> changes from 0 to 1. Why does this not happen?
 
It does happen, expression 'vec[i]' will evaluate to reference of i-th
element of vec. Just that references can not be used as aliases
of such expressions.
 
> I suppose that, if I do want this behaviour, the best technique
> is via lambda functions. Is that correct?
 
Yes, Alf already did show how to declare lambda as alias to expression
'v[i]' and how to use it.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jul 08 06:12PM +0200

On 08-Jul-15 3:32 PM, Stefan Ram wrote:
>> is not true, reference is always reference to concrete object.
 
> At least an /lvalue/ reference.
 
> There can be rvalue references to function type (8.3.2).
 
Yes, and there can be ordinary C++03 reference to function type, also.
 
Unfortunately Visual C++ does not support implicit lambda conversion to
function reference -- it complains about all the Microsoft calling
convention possibilities that it thinks constitute an ambiguity -- so
still at this point in time the syntax gets pretty awkward:
 
<code>
#include <iostream>
 
void invoke( void (&f)() )
{
f();
}
 
void rinvoke( void (&&f)() )
{
f();
}
 
void foo() { std::cout << "foo\n"; }
 
auto main() -> int
{
invoke( foo );
// invoke( *[]{ std::cout << "lambda 1\n"; } ); // Works
with g++ 5.1
invoke( *static_cast<void(*)()>( []{ std::cout << "lambda 1\n"; } )
); // VC 2015
 
rinvoke( foo );
rinvoke( *static_cast<void(*)()>( []{ std::cout << "lambda 2\n"; } ) );
}
</code>
 
I suspect that this code is formally invalid because there's no cast to
rvalue reference, and so the * dereferencing is an lvalue expression
that shouldn't bind to an rvalue reference. But both g++ and VC accept
this. So possibly there's something I didn't think of.
 
Cheers & hth.,
 
- Alf
 
 
--
Using Thunderbird as Usenet client, Eternal September as NNTP server.
ram@zedat.fu-berlin.de (Stefan Ram): Jul 08 01:32PM

>It feels that you expect reference to be alias of expression. That
>is not true, reference is always reference to concrete object.
 
At least an /lvalue/ reference.
 
There can be rvalue references to function type (8.3.2).
 
#include <iostream>
#include <ostream>
#include <string>
#include <random>
 
using namespace ::std::literals;
 
void f() { ::std::cout << "hello, world\n"s; }
 
int main (){ void( &&r )() = f; r(); }
 
>On case of 'int&' it becomes reference to some int when defined
>and stays like that forever.
 
Not forever, only during its lifetime. For example, the function
containing it, might be called several times.
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: