Boost logo

Boost :

From: Michael Fawcett (michael.fawcett_at_[hidden])
Date: 2006-12-04 16:14:32

On 12/3/06, Daniel Wesslén <daniel_at_[hidden]> wrote:
> Not all swizzlings are named, but if the one you happen to want is not
> provided then it is easy to create:
> const indexer<bits::index_swizzle<0,0,2,0> > xxzx;
> const indexer<bits::index_swizzle<3,2,1,0> > wzyx;
> vector<4> v1, v2(1,2,3,4);
> v1 = v2[xxzx]; // v1 = (1,1,3,1)
> v1[xxzx] = v2; // error - v1[xxzx] is not a lvalue due to aliasing
> v1[wzyx] = v2; // v1 = (4,3,2,1)

I really dislike that syntax. Feel free to make use of the swizzle
code generation macros I posted in the Vault. They generate all
possible combinations so users don't have to make instances of your
index_swizzle classes. You would want to replace the SWIZZLE_BODY
macro with your own. It simply takes in the next permutation as a
sequence, e.g. (z)(w)(y).

The only downside is that there will be no compile-time error in
situations like your //error line from above.

v1.xxzx() = v2; // compiles, but doesn't make too much sense

You could try to determine how the function was being used (read vs
write) and only allow write masks for combinations where each vector
component was only specified once:

v1.xxzx() = v2; // fails to compile
v1.wxzy() = v2; // compiles

or you could throw an exception from the assignment operator if any of
the invoking class's references were aliased. I much prefer the
compile-time solution (if one exists?).

--Michael Fawcett

Boost list run by bdawes at, gregod at, cpdaniel at, john at