Assume you are providing a client library with a function that has multiple reference arguments.
For the sake of simplicity lets assume we have 3 arguments and those are various references to int
(so you can assume, that these constants would also be created by initializer_list
).
From a call site it should be possible to pass ad hoc created constants (rvalue) to functions (think of testing code in this case) as well as real references to objects being owned by another entity.
So far I've come up with the following solutions:
void bar(int &x, int &y, int &z) { }
// use a template wrapper to turn rvalues into lvalues.
template <typename T, typename U, typename V>
void foo(T &&t, U &&u, V &&v) {
bar(t,u,v);
}
// create a humongous amount of overloads
void baz(int &&x, int &y, int &z) { }
void baz(int &x, int &&y, int &z) { }
void baz(int &&x, int &&y, int &z) { }
void baz(int &x, int &y, int &&z) { }
void baz(int &&x, int &y, int &&z) { }
void baz(int &x, int &&y, int &&z) { }
void baz(int &&x, int &&y, int &&z) { }
// claim ownership of the objects and create temporaries
void bam(int x, int y, int z) { }
int main() {
int i = 1;
foo(i,2,3);
foo(2,i,3);
foo(2,3,i);
bar(i,2,3); // doesn't compile
bar(2,i,3);
bar(2,3,i);
baz(i,2,3); // requires 8 overloads
baz(2,i,3);
baz(2,3,i);
return 0;
}
I'm not completely satisfied with all of the solutions as every one of them has drawbacks. Is there a cleaner alternative to this problem?