Re: C++0x: move() & a hypotheticyl language feature
David Abrahams wrote:
I like it! Write a proposal!
Alberto Ganesh Barbati wrote:
Vidar Hasfjord ha scritto:
On Nov 10, 6:31 pm, SG <s.gesem...@gmail.com> wrote:
[...]
Disclaimer: I'm going to describe a *hypothetical* language
feature that solves these kinds of "dangling reference"is not
problems.
[...]
string&& operator+(string&& a : return, string const& b);
Interesting.
Here's an alternative syntax based on the new C++0x function
declaration syntax:
[] operator + (string&& a, string const& b) -> a;
Very nice. Please notice that the "unified" function declaration syntax
(the one using []) was not voted into the CD. However, the idea is still
good, if you replace [] with auto:
auto operator+(string&& a, string const& b) -> a;
Although probably less useful, we might also want to introduce a similar
syntax for member functions:
struct A
{
auto operator=(const A&) -> this // returns A&
};
the syntax should behave nicely with cv- and ref-qualifiers:
struct A
{
auto f() -> this // returns A&
auto g() & -> this // returns A&
auto g() && -> this // returns A&&
auto h() const -> this // returns const A&
};
Nice! I actually didn't expect such a positive response. I thought
about this idea alot during the past two days and made some
observations:
* This feature would introduce a new class of function types that must
be supported by function pointers etc.
* Unfortunately, I don't have a solution for perfect forwarding yet for
such functions.
* It could be extended to non-references (pass by value for argument
and return). This allows a compiler to elide one copy/move.
* I think it's desirable to have a returned reference behave exactly
like the original argument in most cases. This would reduce the need
to overload on &/&& parameters. It is believed to be convenient and
less error-prone. (See examples below)
* This feature should allow overriding the above "convenience rule".
Think of std::move that always results in an rvalue expression.
Think of std::forward that should
Here are a couple of examples I currently favour in terms of syntax
and semantics:
struct A {
auto operator+=(int) -> this;
auto move() -> this : A&&;
}
auto foo(A x) -> x; // pass by value for argument and result
// default behaviour = caller
auto bar(A && x) -> x; // implicitly converts reference
// to the original "value-ness"
A source();
A& getref();
void demo() {
// "convenience rule"
bar(source()); // rvalue expression (known temporary)
bar(getref()); // lvalue expression
source()+=23; // rvalue expression (known temporary)
getref()+=42; // lvalue expression
}
template<RvalueOf T>
auto move(T && x) -> x : RvalueOf<T>::type&&
tempalte<IdentityOf T>
auto forward(T && x) -> x : IdentityOf<T>::type&&
Cheers!
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]