Re: Effeciency

From: VANNA CHHUM (euclidonomy_at_verizon.net)
Date: 01/27/04


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



Relevant Pages

  • Re: strange compiler message
    ... >> int main ... strcpy(blah, " blah blah "); ... When I use the wrong signature the compiler issues the following error: ... All of this because C++ introduces the concept of calling a constructor ...
    (comp.lang.cpp)
  • Re: Tcl_CreateCommand in 8.4.4
    ... Tcl_CmdProc typedef is now a bit different. ... typedef int _ANSI_ARGS_((ClientData clientData, ... This way the compiler complains SomeCmd sig ...
    (comp.lang.tcl)
  • Re: Working with strings in c++
    ... int i; ... The use of a copy constructor to initialise myStr from the temporary ... compiler to do it the two-step way, or the compiler is allowed to refuse to ... explicit MyString(const char* arg) ...
    (microsoft.public.vc.language)
  • Re: Requesting sample code!
    ... int m_i1, m_i2; ... then a constructor is not made automatically by the compiler. ...
    (microsoft.public.vc.language)
  • Re: Fooling C Compiler
    ... I tried using typedef. ... Is there a possibility to fool your doctor ... I assume "fooling the compiler" was meant to be ...
    (comp.lang.c)