Re: Effeciency
From: VANNA CHHUM (euclidonomy_at_verizon.net)
Date: 01/27/04
- Next message: Patrik Stellmann: "Re: Optimisation needed"
- Previous message: Andre Kostur: "Re: Optimisation needed"
- In reply to: David Rasmussen: "Re: Effeciency"
- Next in thread: David Rasmussen: "Re: Effeciency"
- Reply: David Rasmussen: "Re: Effeciency"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 27 Jan 2004 07:42:32 GMT
"David Rasmussen" <david.rasmussen@gmx.net> wrote in message
news:bv3rov$ull$1@news.net.uni-c.dk...
> Ron Natalie wrote:
> >
> > The compiler generates the copy-assignment operator and the
> > copy constructor for him. As lame as his class is, the compiler
> > generated ones are just fine.
> >
>
> My class is lame?
>
> What I am trying to achieve here is something sorely lacking in C++, but
> available in other languages, e.g. Ada.
>
> What I really want, is just for the compiler to do some more work, to
> distinguish between my class and an int. That is, more typesafety. This
> is a compile time thing only. The compiler ensures that for example
> operator + isn't used on my class. When the program is ready for
> release, all MyClass instances could be replaced with just variables of
> the type of the encapsulated integer. This could be done easily with a
> typedef.
>
> The effect is that I ensure type safety, and I get the effeciency of the
> integer in release builds. But ideally, there shouldn't be any
> performance difference between the two. This class is what the language
> forces me to do in order to get some very basic type safety done by the
> compiler.
>
> > However, the presence of the constructor from unsinged int means
> > the default constructor is supressed. He probably wants that one.
>
> The empty default constructor? Yes.
>
> /David
Hi,
If I understand what you are saying...you want to typify integral types at
compile time to enforce more type safety. Then, you want to replace that
check with an actual integral type (via a typedef) for your release mode
(max efficiency).
If that is what you want, then look at the code below (sloppy - but gets
the point across):
#include <iostream>
template <typename Underlying, int MixType>
struct MyClass {
explicit MyClass(Underlying val) : myVal(val) { /* */ };
friend MyClass operator*(const MyClass& m1, const MyClass& m2) {
return(static_cast<MyClass>(m1.myVal * m2.myVal)); }
friend MyClass operator+(const MyClass& m1, const MyClass& m2) {
return(static_cast<MyClass>(m1.myVal + m2.myVal)); }
// etc.
friend std::ostream& operator<<(std::ostream& os, const MyClass& m) { os
<< m.myVal; return(os); }
private:
Underlying myVal;
};
// Make as many types you want here
typedef MyClass<unsigned long, 1> FirstIntType;
typedef MyClass<unsigned long, 2> SecondIntType;
// Later replace for maximum efficiency
// typedef unsigned long FirstIntType;
// typedef unsigned long SecondIntType;
int main()
{
FirstIntType a(1), b(2);
SecondIntType c(4), d(5);
std::cout << a * b << std::endl; // ok
std::cout << c * d << std::endl; // ok
std::cout << a * c << std::endl; // error ==> can't mix
std::cout << a * 4 << std::endl; // error ==> can't mix
return(0);
}
Good Luck,
Shane
- Next message: Patrik Stellmann: "Re: Optimisation needed"
- Previous message: Andre Kostur: "Re: Optimisation needed"
- In reply to: David Rasmussen: "Re: Effeciency"
- Next in thread: David Rasmussen: "Re: Effeciency"
- Reply: David Rasmussen: "Re: Effeciency"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|