Home » Developing U++ » U++ Developers corner » Should the pick semantics be changed?
Re: Should the pick semantics be changed? [message #42317 is a reply to message #42295] |
Fri, 07 March 2014 09:00   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
OK, I was playing a bit with &&:
#include <Core/Core.h>
using namespace Upp;
struct Foo {
int a;
Foo(Foo&& x) { LOG("MOVE"); a = x.a; }
void operator=(Foo&& x) { LOG("MOVE="); a = x.a; }
Foo() {}
Foo(const Foo& x, int) { LOG("COPY"); a = x.a; }
};
template <class T>
T&& pick(T& a) { return static_cast<T&&>(a); }
template <class T>
T clone(const T& a) { T c(a, 1); return c; }
struct Bar {
Foo a, b;
Bar(Bar&&) = default;
Bar& operator=(Bar&&) = default;
Bar() = default;
Bar(const Bar& b, int) : a(b.a, 1), b(b.b, 1) {}
};
Bar test_bar()
{
LOG("In FN");
Bar x, y;
x.a.a = 123;
y.a.a = 421;
return Random(2) ? pick(x) : pick(y); // This is somewhat unexpected and ugly...
}
Foo test() {
Foo h;
h.a = 22;
return h;
}
CONSOLE_APP_MAIN
{
{
Foo y;
Foo z = pick(y);
LOG("----------");
Foo x = clone(z);
LOG("----------");
x = pick(y);
LOG("----------");
y = clone(x);
LOG("----------");
x = test();
}
LOG("=======================");
{
Bar y;
Bar z = pick(y);
LOG("----------");
Bar x = clone(z);
LOG("----------");
x = pick(y);
LOG("----------");
y = clone(x);
LOG("----------");
x = test_bar();
}
}
I think this route is actually superior to what we have now, with all operations being stated directly where it matters.
Interestingly, 'clone' would work even with C++03 fine and is clearly superior to Foo x(y, 1); notation, perhaps even to operator<<=...
What sort of surprised me is the need to use 'pick' in test_bar. IMO, this makes that original purpose of && sort of moot...
Mirek
|
|
|
 |
|
Should the pick semantics be changed?
By: piotr5 on Tue, 04 March 2014 11:00
|
 |
|
Re: Should the pick semantics be changed?
By: mirek on Tue, 04 March 2014 12:12
|
 |
|
Re: Should the pick semantics be changed?
By: piotr5 on Tue, 04 March 2014 23:14
|
 |
|
Re: Should the pick semantics be changed?
By: mirek on Wed, 05 March 2014 08:54
|
 |
|
Re: Should the pick semantics be changed?
By: piotr5 on Wed, 05 March 2014 10:27
|
 |
|
Re: Should the pick semantics be changed?
By: mirek on Wed, 05 March 2014 12:02
|
 |
|
Re: Should the pick semantics be changed?
By: mirek on Fri, 07 March 2014 09:00
|
 |
|
Re: Should the pick semantics be changed?
By: piotr5 on Fri, 07 March 2014 16:23
|
 |
|
Re: Should the pick semantics be changed?
By: mirek on Fri, 07 March 2014 16:44
|
 |
|
Re: Should the pick semantics be changed?
By: mirek on Sun, 09 March 2014 09:34
|
 |
|
Re: Should the pick semantics be changed?
By: piotr5 on Sun, 09 March 2014 13:28
|
 |
|
Re: Should the pick semantics be changed?
By: Lance on Sun, 23 March 2014 22:02
|
 |
|
Re: Should the pick semantics be changed?
By: piotr5 on Tue, 25 March 2014 19:35
|
 |
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 02:37
|
 |
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 02:48
|
 |
|
Re: Should the pick semantics be changed?
By: piotr5 on Wed, 26 March 2014 10:35
|
 |
|
Re: Should the pick semantics be changed?
By: mirek on Wed, 26 March 2014 11:38
|
 |
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 21:25
|
 |
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 21:21
|
Goto Forum:
Current Time: Tue Apr 29 16:15:04 CEST 2025
Total time taken to generate the page: 0.00592 seconds
|