search of a workaround
Namespace
rswhite4 at googlemail.com
Fri Feb 8 13:06:24 PST 2013
I've been thinking about the lack of rvalue references.
Therefore, I would like to know what you think is the best
workaround for this feature.
I illustrate below the only (*) five solutions briefly and
summarized.
What do you think is the best? Which of these do you use? Or did
I forget some possible solutions?
I'm looking for this for small data structures, such as vectors,
matrices, or colors.
(*) In my opinion.
--------
struct A { }
---- Solution #1 ----
// rvalues
void foo(A a) {
foo(a);
}
// lvalues
void foo(ref A a) {
}
----------------------------------
Summarized: flexible, efficient but code bloat, more effort and
bug prone:
void foo(A a) {
writeln("call no ref");
foo(a);
}
void foo(const ref A a) {
writeln("call ref");
}
void main() {
foo(A());
}
-> endless loop!
---- Solution #2 ----
// fuck it, copy lvalues and rvalues
void foo(A a) {
}
----------------------------------
Summarized: flexible, no code bloat but inefficient ->
unnecessary copies
---- Solution #3 ----
// allow only lvalues
void foo(ref A a) {
}
----------------------------------
Summarized: You have to make temporary values by yourself:
unhandy, ugly and code bloat
---- Solution #4 ----
// accept only pointers
void foo(A* a) {
}
----------------------------------
Summarized: C Style, nullable and same problems as with solutions
#3.
---- Solution #5 ----
// Use classes
class A { }
// Works for lvalues and rvalues
void foo(A a) {
}
----------------------------------
Summarized: flexible, efficient, no code bloat and same creation
(with static opCall) as structs. But: heap allocations and
nullable.
More information about the Digitalmars-d-learn
mailing list