Re: Another approach to forward/move issues
on Fri Mar 16 2007, grizlyk1-AT-yandex.ru ("Grizlyk") wrote:
tasjaevan@gmail.com wrote:
Tell me please, "r-value reference" is "moveable reference" or "moveable
value"? There are great differences between "value" and "reference" data
types in C++. Any "reference" is always address "sizeof(r-value
reference)==sizeof(reference)". What "sizeof(T&&)" is? Consider
_simplest_
example with moveable:
An r-value reference is mostly identical to a regular (lvalue)
reference. It doesn't directly mean 'moveable', but it does enable
move semantics.
I just can repeat (and sure, most C++ programmers agree with me) that
reference is not the same as value and you never can invent any kind of
"special reference" to be simultaneously "value" and "reference" for any
kind of semantics.
When Howard writes "regular (lvalue) reference" you can read it simply
as "reference as defined in C++98." You shouldnt' be distracted by
the appearance of the word "value" in "lvalue." That's merely a way
of reminding ourselves that C++98 references are treated as (and
indistinguishable from ) lvalues in most contexts.
At least for "move semantics" we have sharp differences between "moveable
passed by reference" and "moveable passed by value" (see
http://grizlyk1.narod.ru/cpp_new #11..#15)
rvalue references indicate "pass by reference"
and removing from C++ manual
control of "what kind of parameter will be used" is very wrong idea,
contradicting to all other parts of C++.
No such removal of control exists in the rvalue reference feature.
I've translated your examples below (but real world
examples would be better than snippets of syntax).
My dictionary of 8000 popular english words does not contain
"snippets", but it does not metter here, because the question "what
kind of syntax is better" is topic to discuss here. It is not a
thread named "Collection of words of love to r-value reference" or
"R-value reference is Only True moveable semantic representation in
the world".
Wow, attitudinal! (also not in your english dictionary)
template<class T>
void foo(T moveable& t)
In the example "t" is "reference" - address of caller parameter of "T" type.
Will be allocated "sizeof(T*)" memory, not "sizeof(T)".
{
t = 3;
//here _no_ end of "t" life-time
}
template <class T>
void foo(T&& t)
{
t = 3;
}
And what is "T&& t" implementation? Is "t" address of exernal "T" or
copy of value of "T"? What size of memory i must allocated on stack
for "foo" to place parameter?
Using semantics of term "reference" i am assuming "T&&" means: "t" is
address of exernal "T". Will be allocated "sizeof(T*)" memory, not
"sizeof(T)".
Correct.
template<class T>
T moveable boo(const T moveable t)
In the example "t" is "value" - new value of caller parameter of "T" type.
Also "boo" declared as returning value created by "move constructor" instead
of "copy constructor".
Does that information belong in the declaration? It is not very
useful to the caller of the function, is it?
{
//here end of "t" life-time
T moveable tmp(t);
tmp = 3;
//here end of "tmp" life-time
return tmp;
}
template <class T>
T boo(T&& t)
No, no, no. You are contradicts youself. Previously we have assumed that
"T&&" means: "t" is address of exernal "T", but now you are trying to
express with "T&&" value of exernal "T", you are trying to express
"sizeof(T)" with the help of "sizeof(T*)" - it is impossible.
Howard does not contradict himself; it seems he's merely attempting to
produce a function with semantics identical to what he supposes your
"boo" is intended to do. The rvalue reference proposal recognizes
that one can implement the same semantics as a moveable value
parameter (at least, by my interpretation of what you mean by
"moveable value) by manually moving from an rvalue reference
parameter, so it uses only one language feature (rvalue reference)
rather than specifying moveable value and moveable reference.
{
T tmp(std::move(t));
tmp = 3;
return tmp;
You just can not do "return tmp", because T::T(const T&) is private in this
context - T is moveable only.
Actually with the rvalue reference feature, a "moveable only" type can
be returned as above; the compiler just moves it. In fact, all types
are returned by move (where move devolves to copy if there's no move
constructor available). So, yes, you can just do "return tmp."
}
The value return type is deliberate.
What does "deliberate" mean in the context? Really, do not
understand any in the sentence.
It means that Howard understood that you meant to move the return
value, and he replicated those semantics without needing to complicate
the return type of his function with a "moveable" notation.
template<class T>
moveable T voo()
{
moveable T x;
foo<T>(x);
//here end of "x" life-time and reassignment of "x"
x=boo<T>(x);
//here end of "x" life-time
return x;
}
template <class T>
T voo()
{
T x;
foo<T>(std::move(x));
x = boo<T>(x); // no std::move required here, since boo returns by
value
return x;
Again: You just can not do "return tmp", because T::T(const T&) is private
in this context - T is moveable only.
Except that you can.
What does "std::move" mean?
It simply turns the expression into an rvalue.
What "rvalue" property are you using here?
The one defined in the standard. More specifically, std::move takes
an (regular C++98, lvalue) reference argument and returns it as an
rvalue reference. As a result, the compiler can treat that reference
as an rvalue, including being able to move from it.
Do not forget - you need allocate concrete size of memory for
functions and variables and there is no any "sizeof(rvalue)" defined
in C++.
I'm not sure what you're claiming, but the expression
5
is an rvalue, and
sizeof(5)
certainly has meaning in C++.
T y;
T x;
x=std::move(y);
I see here two explicit operations:
x.operator=( from std::move<T>_return_value )
No, std::move only affects the rvalue-ness of the expression. It does
nothing by itself.
What is "rvalue-ness" of any expression?
See the C++ standard.
What implementation of "std::move" must be to do "rvalue-ness"?
See
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1690.html:
template <class T>
inline
typename remove_reference<T>::type&&
move(T&& t)
{
return t;
}
[ http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1690.html#A%20Minor%20Error%20in%20N1377
explains the use of remove_reference ]
I am guessing, "std::move" is going to explicitly try to tell to compiler
that "move assignment/constructor" must be used insted of "copy
one".
Replace "must" with "can," and you have it right.
But what to do with implicit assignment/constructions with move
semantics?
I don't understand the question.
Why don't you read the proposal? In fact, it's a
single operation: T's move assignment operator.
I read it many times but can not understand it because either
semantics of used terms contradicts to the same ordinary C++ terms
Which terms? If you ask specific questions, I'm sure someone can
clarify things for you.
or i even can not imaging how a property of so simple thing as
"moveable balue and reference" can be implemented in the proposal
even in theory.
I think you would have to very clearly explain what you mean by
"movable value and reference" before anyone should try further to map
the proposal onto those concepts.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]