473,563 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3699
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::valarra y. 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
2020
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
2357
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 http://www.angelikalanger.com/Articles/Cuj/ExpressionTemplates/ExpressionTemplates.htm It provide an express template for only one argument (see Listing 19 and so on), which can be used to do numerical...
11
1312
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; int b = (++a) * ((++a)+2); printf( "%d %d", ++b, ++b );
2
1921
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 higher than the lower corner. I write a code as below class Domain { private:
6
5313
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. Here's a very simple example which should illustrate what I'm doing. #include <iostream> template <class T> class TestBase {
25
1986
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
4756
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 appear in a constant-expression" template <int s> class CFoo { private:
2
2437
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 expression template. template <size_t N, class T> class DotProduct {
10
4517
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 integer, it should return false, but it should work normally if the parameter is a float. To illustrate this, I attached a minimal example.
0
7659
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.