|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - Compiling problem.
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I have some code which compiles and runs sometimes, with Digital Mars.
The problem is one of the use of overloaded operators within a class. I
will discuss this in relation to the following class definition.
template < class T > class X
{
T t;
public :
X() : t(T(0)) { }
X(T tt) : t(tt) { }
~X() { }
T gett() const { return t; }
X<T> operator +(const X&);
X<T> operator -(const X&);
friend X<T> operator *(const X&,const X&);
};
I have been using code like that for operator + and - above, where the
operator is a member function rather than an operator. The point is
that it compiled and ran until the other day when I started doing things
such as
ans = a+b+c;
when it mysteriously crashed. I have traced this to the fact that the
friend operator does much more work with registers BETWEEN and AFTER the
calls.
This is an issue of how to go about things - Stroustrup in "The Design
and Evolution of C++" p.81 recommends the friend operator.
But it is also an issue of code which runs and does funny things. I
attach files
xa.h xatest.cpp and xatest.unm (unmangled machine code)
Note that the example corrupts one of the input arguments:
Digital Mars version is 2050
a = 1, b = 2, c = 3
a = -1, b = 2, c = 3
a+b+c = 6
a*b*c = 6
a-b-c = -4
John Fletcher
Mar 29 2001
Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit John Fletcher wrote:xa.h xatest.cpp and xatest.unm (unmangled machine code) John Fletcher Mar 29 2001
John Fletcher wrote:I have some code which compiles and runs sometimes, with Digital Mars. The problem is one of the use of overloaded operators within a class. I will discuss this in relation to the following class definition. I have been using code like that for operator + and - above, where the operator is a member function rather than an operator. The point is that it compiled and ran until the other day when I started doing things such as Mar 29 2001
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
There is more to this than I thought.
Look at the following.
// Type 2 - friend function.
template <class T>
X<T> operator * (const X<T> &arg1, const X<T> &arg2)
{
return X<T>(arg1.t*arg2.t);
}
// Type 2a - friend function with temporary
template <class T>
X<T> operator + (const X<T> &arg1, const X<T> &arg2)
{
X<T> temp(arg1.t+arg2.t);
// Adding something like this changes the program behaviour.
cout << temp.gett() << endl;
return temp;
}
The print statement in the second function modifies the program behaviour in my
full
example. In that case without the print the program hangs for 30 seconds.
In the short example without the print, the two functions compile to the same
machine
code. I am doing no optimisations..
Attachments xb.h and xbtest.cpp
John
Mar 29 2001
Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Here is an extract from the disassembled code which is operating in my full example. The code hangs unless the print statement is compiled. John Mar 29 2001
I compiled your example with:
sc xatest -o -XD
and
sc xatest -XD
with version 8.1B2n, and it compiled and ran successfully both times.
Specifically:
E:\bug>sc xatest -XD -o
link test,,,user32+kernel32/noi;
E:\bug>xatest
Digital Mars version is 2066
a = 1, b = 2, c = 3
a = -1, b = 2, c = 3
a+b+c = 6
a*b*c = 6
a-b-c = -4
E:\bug>scppn
Digital Mars C/C++ Compiler Version 8.1B2n
Copyright (C) Digital Mars 2000-2001. All Rights Reserved.
Written by Walter Bright
www.digitalmars.com
John Fletcher wrote in message <3AC315EB.62C03124 aston.ac.uk>...
Mar 29 2001
Walter wrote: Mar 30 2001
John Fletcher wrote in message <3AC4538B.7065E4FA aston.ac.uk>...By the way, what is the significance of the numbers returned by __DMC__? Mar 30 2001
Walter wrote:John Fletcher wrote in message <3AC4538B.7065E4FA aston.ac.uk>...By the way, what is the significance of the numbers returned by __DMC__? Mar 30 2001
In iostream.h, there's a flag called 'hex' and some associated functions to get/set the flag. Myself, I just use printf. John Fletcher wrote in message <3AC467C8.90A24BA6 aston.ac.uk>...Walter wrote:John Fletcher wrote in message <3AC4538B.7065E4FA aston.ac.uk>...By the way, what is the significance of the numbers returned by __DMC__? Mar 30 2001
|