Re: Constrained Forwarding(R-Value Reference)
Pedro Lamar=E3o wrote:
template <class It> It& increment(It&);
"It&" means "reference to copyable", so you have required much from th=
e
type
"It" to be "copyable". Any "moveable" (as auto_ptr) can not be used fo=
r
the
template.
lt& doesn't mean "reference to copyable", it means "reference to
lvalue".
He has asked me how it can be implemented with my proposal. Try open
http://grizlyk1.narod.ru/cpp_new and find there words "lvalue". Really,
lvalue/rvalue has ablsolutely no any sense in context of data type. Lvalu=
e
means "address can be taken" and nothing more. "Lvalue" is unrelated to
"const", "copyable" or "moveable" data type.
As i can understand, temporary can not be assigned to "non-const referenc=
e"
only due to the reference can be passed out of the temporary scope and
writing into non-existing stack of the terminated function for some peopl=
e
looks like more dangerous when reading from the non-existing memory or
calling functions with random entry point, that can be done with the
temporary assigned to "const reference".
template <class It, class T>
It
foo(It first, It last, const T& x)
{
"const T&" means "reference to const copyable", so you have required f=
rom
the type "T" to be "copyable". Any "moveable" (as auto_ptr) can not be=
used
for the template.
Just declaring the parameter as const T& doesn't require T to be
Copyable.
Who does argue? In order to declare moveable parameter we need write "con=
st
moveable T&", It means "reference to const moveable". Or we need write
"moveable T&", It means "reference to moveable".
The following type:
struct resource {
resource ();
int query (int param) const; // query resource on certain param
Are you trying to change "param"?
private:
resource (resource const&);
resource& operator= (resource const&);
void* stuff;
};
is not Copyable.
I agree, because "copy constructor" is private.
Now let me fill in this foo function:
template <typename Iterator, typename T>
Iterator
foo (Iterator begin, Iterator end, T const& t) {
You have required "Iterator" and "T" to be copyable.
for (; begin != end; ++begin) {
int result = t.query(*begin);
// operate with result somehow.
}
return begin;
}
But you have never used the requirement for "T" in foo<>.
Now, observe the following:
vector<int> params;
bar b;
foo(params.begin(), params.end(), b);
b is an lvalue binding to a const referente to lvalue,
b is copyable lvalue binding to a const reference to copyable
and b is of type bar, and bar is not Copyable.
In spite the "bar" is undeclared, i trust you.
That declaration does not require the type bar to be Copyable.
Because you have never used the requirement to be copyable for "T" in foo=
<>.
references to lvalues are not necessarily references to values of
Copyable types.
There are no reference to lavalue in the my proposals. Reference to
non-copyable (simultaneously non-moveable) can not be explicitly declared=
..
Do you need the declaration?
--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new
---
[ 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 ]