473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to check validity in expression template

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
3 3684
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rex_chaos | last post by:
Hi all, As some book tells, I try the following example of expression template. template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
1
by: PengYu.UT | last post by:
Hi, I read Klaus Kreft & Angelika Langer's C++ Expression Templates: An Introduction to the Principles of Expression Templates at...
11
by: Kufa | last post by:
Hello, I am wondering of the validity of those lines, not meaning i'm using them, but i cant point out in the norm if they are legal, and in such a case the expected behaviour. int a = 2;...
2
by: shuisheng | last post by:
Dear All, Assume I have a class for a cuboid domain. The domain is defined by the cuboid's lower corner, such as (0, 0, 0), and upper corner, such as (1, 1, 1). The upper corner should be always...
6
by: Lawrence Spector | last post by:
I ran into a problem using g++. Visual Studio 2005 never complained about this, but with g++ I ran into this error. I can't figure out if I've done something wrong or if this is a compiler bug. ...
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
3
by: Dan Smithers | last post by:
What constitutes a constant-expression? I know that it is something that can be determined at compile time. I am trying to use template code and keep getting compiler errors "error: cannot...
2
by: madhu.srikkanth | last post by:
Hi, I came across a paper by Angelika Langer in C++ Users Journal on Expression Templates. In the article she had mentioned that the code snippet below used to calculate a dot product is an...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.