P
Paul Davis
I'd like to overload 'comma' to define a concatenation operator for
integer-like classes. I've got some first ideas, but I'd appreciate a
sanity check. The concatenation operator needs to so something like
this:
1) e = (a, b, c, d); // concatenate a,b,c,d into e
2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d
For example, in the second case, assume that a,b,c,d represent 2-bit
integers, and e represents an 8-bit integer. If e contains 0b10110100,
then after the assignment, a contains 0b10, b contains 0b11, and so
on.
My current thinking, for the second case, is:
1) Overload comma for the new integer class. This needs several (3?)
prototypes, because the operands may be const temporaries.
2) In the second example, 'a,b' is evaluated first. This results in a
temporary, so I return a const reference to the temporary. This
requires this operator:
const wibble& operator , (wibble&, wibble&) // proto 1
3) '(temporary , c)' is then evaluated. This requires this operator:
const wibble& operator , (const wibble&, wibble&) // proto 2
4) Eventually, we end up with a temporary which has 'e' assigned to
it, which needs this operator:
const wibble& operator= (wibble&) // proto 3
(or just void operator= (wibble&)?)
If this makes sense, my first problem is how to get the value of 'e',
which is only seen by operator=, into the the a, b, c, and d objects.
Any ideas? Currently, my only idea is that the comma operator should
store a reference to its operands in a global list somewhere, and
operator= then runs through the list assigning to all the wibbles in
it. This doesn't feel good, and there's also the question of when to
initialise the list. However, I guess I could do this with the comma
operators which have a non-const left operand, since they should
always be used for the first comma evaluation.
Next question: what if the user wants to do
(a, (b, c), d) = e;
Is this possible? Does the temporary result of (b,c) go out of scope
before it can be used to evaluate (a, (b,c))? If this is Ok,
presumably it would require a third comma operator:
const wibble& operator , (wibble&, const wibble&)
Does any of this make sense?
Many thanks
Paul
______________________________________
To get a valid mail address: s/m@/m1@/
integer-like classes. I've got some first ideas, but I'd appreciate a
sanity check. The concatenation operator needs to so something like
this:
1) e = (a, b, c, d); // concatenate a,b,c,d into e
2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d
For example, in the second case, assume that a,b,c,d represent 2-bit
integers, and e represents an 8-bit integer. If e contains 0b10110100,
then after the assignment, a contains 0b10, b contains 0b11, and so
on.
My current thinking, for the second case, is:
1) Overload comma for the new integer class. This needs several (3?)
prototypes, because the operands may be const temporaries.
2) In the second example, 'a,b' is evaluated first. This results in a
temporary, so I return a const reference to the temporary. This
requires this operator:
const wibble& operator , (wibble&, wibble&) // proto 1
3) '(temporary , c)' is then evaluated. This requires this operator:
const wibble& operator , (const wibble&, wibble&) // proto 2
4) Eventually, we end up with a temporary which has 'e' assigned to
it, which needs this operator:
const wibble& operator= (wibble&) // proto 3
(or just void operator= (wibble&)?)
If this makes sense, my first problem is how to get the value of 'e',
which is only seen by operator=, into the the a, b, c, and d objects.
Any ideas? Currently, my only idea is that the comma operator should
store a reference to its operands in a global list somewhere, and
operator= then runs through the list assigning to all the wibbles in
it. This doesn't feel good, and there's also the question of when to
initialise the list. However, I guess I could do this with the comma
operators which have a non-const left operand, since they should
always be used for the first comma evaluation.
Next question: what if the user wants to do
(a, (b, c), d) = e;
Is this possible? Does the temporary result of (b,c) go out of scope
before it can be used to evaluate (a, (b,c))? If this is Ok,
presumably it would require a third comma operator:
const wibble& operator , (wibble&, const wibble&)
Does any of this make sense?
Many thanks
Paul
______________________________________
To get a valid mail address: s/m@/m1@/