By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

how to check validity in expression template

P: n/a
Hi there,
I have a question about using expression template. We know that the
final calculation in expression template will end up with a series of
element-by-element operations. The concept can be explained with

W = X o Y o Z; (here o denotes any operator)

W.operator = LOR(LOR<X, o, Y>, o, Z);

and in W.operator=, we have
for ( int i=0; i<length_of_W; i++ ) W[i] = X[i] o Y[i] o Z[i];

The expression above confusing me!!! How can we check if the operands
are "valid"? For example, we are suming up three vectors W = X+Y+Z;

How can we know if both of the vectors are of the same dimension?
Maybe it is still a open issue. I have a look at the source of
stl::valarray. I found no code has been added to check such validity.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 27 Oct 2003 03:07:04 -0800, re*******@21cn.com (Rex_chaos) wrote:
Hi there,
I have a question about using expression template. We know that the
final calculation in expression template will end up with a series of
element-by-element operations. The concept can be explained with

W = X o Y o Z; (here o denotes any operator)

W.operator = LOR(LOR<X, o, Y>, o, Z);

and in W.operator=, we have
for ( int i=0; i<length_of_W; i++ ) W[i] = X[i] o Y[i] o Z[i];

The expression above confusing me!!! How can we check if the operands
are "valid"? For example, we are suming up three vectors W = X+Y+Z;

How can we know if both of the vectors are of the same dimension?
Maybe it is still a open issue. I have a look at the source of
stl::valarray. I found no code has been added to check such validity.


You don't check - you just make it undefined behaviour to get it
wrong. Any check you add will hurt performance, so it's best to just
make it up to the user of the library to get it right. You could have
some debug asserts, of course, in the operator[] functions.

Tom
Jul 19 '05 #2

P: n/a
> You don't check - you just make it undefined behaviour to get it
wrong. Any check you add will hurt performance, so it's best to just
make it up to the user of the library to get it right. You could have
some debug asserts, of course, in the operator[] functions.

How can I have a code for checking valid in operator[]? We know
nothing about the operand (the container). What we know is the element
!
Jul 19 '05 #3

P: n/a
On 27 Oct 2003 09:53:42 -0800, re*******@21cn.com (Rex_chaos) wrote:
You don't check - you just make it undefined behaviour to get it
wrong. Any check you add will hurt performance, so it's best to just
make it up to the user of the library to get it right. You could have
some debug asserts, of course, in the operator[] functions.How can I have a code for checking valid in operator[]?


It depends on the operator[]. If you wrote it, just add the check. If
you didn't, then you can't.

We knownothing about the operand (the container). What we know is the element
!


I'm saying that the operator[] functions for the containers should do
their own (debug) checking. If they don't, and you can't change them,
then you'll need some way of getting the size out of a "container".
e.g.

You could possibly do:

assert(sequence_traits<Xtype>::size(X) == length_of_W);
assert(sequence_traits<Ytype>::size(Y) == length_of_W);
assert(sequence_traits<Ztype>::size(Z) == length_of_W);
for ( int i=0; i<length_of_W; i++ )
W[i] = X[i] o Y[i] o Z[i];

for suitably defined sequence_traits. What kinds of things could X, Y,
and Z possibly be? std::valarray? valarray::slice? std::vector? Plain
arrays? It would be easy to add traits that just called .size() to get
the size in the general case, and partially specialize for arrays if
necessary.

Tom
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.