473,761 Members | 9,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

porting expression templates to generics (2)

Hi!

I've changed the code to use Apply instead of operator() and now I get
this errors:
(1) left of '.Apply' must have class/struct/union
(2) left of '.GetRowNum' must have class/struct/union
(3) 'return' : cannot convert from
'Expression<Lhs ,Rhs>' to 'Expression<Lhs ,Rhs>'
Cannot copy construct struct 'Expression<Lhs ,Rhs>' due to
ambiguous copy constructors or no available copy constructor
public interface class IExpression
{
public:
typedef System::Single tip;
virtual tip Apply(int r, int c);
virtual int GetRowNum();
};

generic <class Lhs, class Rhs>
where Lhs:IExpresion
where Rhs:IExpresion
ref struct Expression : public IExpresion
{
typedef float tip;
Expression( Lhs% lhs, Rhs% rhs)
: l_(lhs), r_(rhs)
{
}
virtual tip Apply (int r, int c)
{
(1) return l_.Apply(r, c) + r_.Apply(r, c);
}

virtual int GetRowNum()
{
(2) return l_.GetRowNum();
}

private:
Lhs l_;
Rhs r_;
}

generic <class Lhs, class Rhs>
where Lhs:IExpression
where Rhs:IExpression
Expression<Lhs, Rhs> operator +(Lhs% lhs, Rhs% rhs)
{
(3) return Expression<Lhs, Rhs>(lhs, rhs);
}

Feb 7 '06 #1
6 1309
Zoran Stipanicev wrote:
generic <class Lhs, class Rhs>
where Lhs:IExpresion
where Rhs:IExpresion
ref struct Expression : public IExpresion
{
private:
Lhs l_;
Rhs r_;
}


Lhs and Rhs are of type IExpression, which are interface types, and
therefore they can't be instantiated. Try

private:
Lhs% l_;
Rhs% r_;

or use handles:

Expression(Lhs^ lhs, Rhs^ rhs) : l_(lhs), r_(rhs) { }
private:
Lhs^ l__;
Rhs^ r_;

Tom
Feb 8 '06 #2
Hi Tom!

I've tried that and the errors are:
error C3160: 'Rhs %' : a data member of a managed class cannot have
this type
error C3229: 'Rhs ^' : indirections on a generic type parameter are not
allowed

Any other idea?

Thx!

Best regards,
Zoran Stipanicev

Feb 8 '06 #3
> I've tried that and the errors are:
error C3160: 'Rhs %' : a data member of a managed class cannot have
this type
error C3229: 'Rhs ^' : indirections on a generic type parameter are not
allowed


It seems that a generic parameter is a handle by itself, and you must
not use the ^ symbol with it. When you write

Lhs l_;

it's actually a handle, and works like if it was declared as

Lhs^ l_;

Therefore you can't use

l_.Assign

you must do

l_->Assign

I find this very confusing, it's a bad thing in my opinion. Declaring
something with stack semantics, which is in fact a handle? I wonder why
they made it like that. If it's a handle, it should have the caret
symbol, and if it's not a handle, it should use the . syntax, not ->. I
seem to be a serious inconsistency in the language.

Well, that covers errors 1 and 2. The 3rd is because you don't have a
copy constructor, and the compiler doesn't create one for you. You have
to write your own. I don't know if this does what you want, but it compiles:

Expression(Expr ession% source)
: l_(source.l_), r_(source.r_)
{
}

Tom
Feb 8 '06 #4
Thx!

I've rewritten the code and it solved those errors but now I get this
one:
stdafx.obj : error LNK2005: "struct DotNetMat::Expr ession<Lhs,Rhs>
__clrcall DotNetMat::oper ator+<Lhs,Rhs>( class MAAABAAB::Lhs
%,class MAAABAAC::Rhs %)
"
(??$?H$RLhs@MAA ABAAB@$RRhs@MAA ABAAC@@DotNetMa t@@$$FYM?AU?$Ex pression@$RLhs@ MAAABAAB@$RRhs@ MAAABAAC@@0@A$C AVLhs@MAAABAAB@ @A$CAVRhs@MAAAB AAC@@@Z)
already defined in dotNET_Sustav_M atrica.obj
...\Debug\dotNE T_Sustav_Matric a.exe : fatal error LNK1169:
one or more multiply defined symbols found

(DotNetMat in error text is a namespace in which
all classes and operators are defined)
I really don't know why this error occurs because there is only one
"operator +"
in whole project.

Here is the new code (I've added constructor and replaced "." with
"->"):
public interface class IExpression {/*same as before*/}

generic <class Lhs, class Rhs>
where Lhs:IExpression
where Rhs:IExpression
ref struct Expression : public DotNetMat::IExp ression
{
typedef float tip;

Expression(Expr ession% org)
: l_(org.l_), r_(org.r_)
{
}

/*Also tried this but the same error occurs
Expression(cons t Expression% org)
: l_( const_cast<Lhs> (org.l_) ), r_(
const_cast<Rhs> (org.r_) )
{
}
*/
Expression( Lhs lhs, Rhs rhs)
: l_(lhs), r_(rhs)
{
}
virtual tip Apply (int r, int c)
{
return l_->Apply(r, c) + r_->Apply(r, c);
}

virtual int GetRowNum()
{
return l_->GetRowNum();
}
public:
Lhs l_;
Rhs r_;

};
generic <class Lhs, class Rhs>
where Lhs:IExpression
where Rhs:IExpression
Expression<Lhs, Rhs> operator +(Lhs% lhs, Rhs% rhs)//same error
with op+(Lhs lhs, Rhs rhs){...}
{
return Expression<Lhs, Rhs>(lhs, rhs);
}

main()
{
Matrix^ lhs = gcnew Matrix(3);//creates matrix 3x3
Matrix^ rhs = gcnew Matrix(3);

//...
System::Console ::WriteLine( (lhs + rhs).Apply(1,1) ); //(lhs +
rhs)->Apply(1,1) can't be used

}
Best regards,
Zoran Stipanicev.

Feb 8 '06 #5
Hi!
About the syntax problem you mentioned, I found this in msdn library:

Handles types and value types may be used as type arguments. In the
generic
definition, in which either type may be used, the syntax is that of
reference
types. For example, the -> operator is used to access members of the
type of the
type parameter whether or not the type eventually used is a reference
type or a
value type. When a value type is used as the type argument, the runtime

generates code that uses the value types directly without boxing the
value
types.

When using a reference type as a generic type argument, use the handle
syntax.
When using a value type as a generic type argument, use the name of the
type
directly.

// generics_overvi ew_2.cpp

// compile with: /clr

generic <typename T>

ref class GenericType {};
ref class ReferenceType {};

value struct ValueType {};
int main() {

GenericType<Ref erenceType^> x;

GenericType<Val ueType> y;

--
Stipanicev

Feb 8 '06 #6
Problem solved!

--
Stipanicev

Feb 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3203
by: Andre | last post by:
Hi, I've built a math library that does some matrix multiplications and other linear algebric functions. I read some place about generics or class templates and I was wondering if this will help me in any way? I have structures and classes with the same logic but that which deal with different types. Moreover, I would like to know if there are any performance benifits or drawbacks and how templates are or can be important to numerical...
7
1606
by: Bob Weiner | last post by:
Hi, Forgive me if this has already been discussed but does C# have a construct like C++ templates? bob
2
3104
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
11
1823
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
2
1274
by: Zoran Stipanicev | last post by:
I'm trying to port simple expression template to generics but I get some strange errors. The code is shown below and the errors are: (1) 'l_' : is not a member of 'IExpresion' (2) syntax error : '(' generic <class Lhs, class Rhs> where Lhs:IExpresion where Rhs:IExpresion ref struct Expression : public DotNetMat::IExpresion
1
1763
by: titan.nyquist | last post by:
"At the implementation level, the primary difference is that C# generic type substitutions are performed at runtime and generic type information is thereby preserved for instantiated objects." - http://msdn2.microsoft.com/en-us/library/c6cyy67b.aspx Wonder if someone could elaborate this difference to me. I understand performing substitutions at runtime is slow. But, it implies in c++ templates, the type information is not preserved...
29
2940
by: Dexter | last post by:
This Java based utility may be invoked from Java code to parse mathematical expressions. It is useful for programmers developing calculators, graphing utilities or other math related programs. Download for free at http://www.thinkanddone.com/prog/java/parser.html If the above link does not work try http://www.britishcomputercolleges.com/prog/java/parser2.html
2
2448
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 {
26
231
by: puzzlecracker | last post by:
Team, C++ has been around since 1986, why templates are still regarded is a new feature by most compiler vendors and not fully supported (for example export feature). Look at other popular languages -- say Java, CSharp --and templates , also known as generics, are fully implemented and supported in latest releases. Yes, in C++ they are implemented differently, yet not better. Then why C++ is so lagging behind. Can't we get ourself...
0
9531
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3881
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
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.