how to check validity in expression template

R

Rex_chaos

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 = X o Y o Z;

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.
 
T

tom_usenet

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 = X o Y o Z;

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
 
R

Rex_chaos

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
!
 
T

tom_usenet

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 know
nothing 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 = X o Y o Z;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top