Topic: hint for a new keyword


Author: "Mycroft Holmes" <holmes@technologist.com>
Date: Tue, 13 Feb 2001 13:00:03 GMT
Raw View
I'm thinking about a new keyword for standard c++, to introduce the concept
of class environments:
let's assume the keyword is "environment" and the syntax is:

environment<class name> instance_name;    // declaration
using environment instance_name;    // activation

The key idea is that an environment is a "place" where static class members
live.
An example:


 class SomeClass
 {
     double* array;
     static int length;

     SomeClass()
     {
         array = new double [ length ];
     }

 };


If I invoke new to get an array of SomeClass, all its members will have the
same length.


 SomeClass::length = 27;
SomeClass* p = new SomeClass [65536];


But now suppose I want SEVERAL arrays, and 'length' may be different from
array to array.
The easy way is to change SomeClass


 class SomeClass2
 {
 private:

     int length_private;        <<<<<<<<<<<<<<<
     double* array;

     static int length;

     SomeClass2()
{
         length_private = length;    <<<<<<<<<<<
         array = new double [ length ];
     }

 };

So I can use:

 SomeClass2::length = 27;
SomeClass2* p = new SomeClass2 [65536];
 SomeClass2::length = 25;
SomeClass2* q = new SomeClass2 [65536];


But SomeClass2 is bigger than SomeClass, because each object contains a copy of the length.
Suppose, instead, I declare an environment (which formally is an object!) and I activate it:


environment<SomeClass> env_1, env_2;
using environment env_1;
SomeClass::length = 17;
using environment env_2;
SomeClass::length = 24;

This is similar to namespace syntax, but since "environment" is an object, the following code is valid



environment<SomeClass> env[14];
 for (int i=0;i<14;i++)
 {
     using environment env[i];
    // do_something
 }




--
 The set of solutions is never empty.
 Two solutions together form a new problem.
-- Mycroft Holmes



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Michiel Salters <salters@lucent.com>
Date: Tue, 13 Feb 2001 15:20:01 GMT
Raw View
Mycroft Holmes wrote:

> I'm thinking about a new keyword for standard c++, to introduce the concept
> of class environments:
> let's assume the keyword is "environment" and the syntax is:

> environment<class name> instance_name;    // declaration
> using environment instance_name;    // activation

Bad idea, new keywords. You'd need quite an impressive case to
convince people that without it reasonable constructs are
impossible.

> The key idea is that an environment is a "place" where static class members
> live. An example:

>  class SomeClass  {
>      double* array;
>      static int length;

>      SomeClass() { array = new double [ length ]; }
>  };


> If I invoke new to get an array of SomeClass, all its members will have the
> same length.

Yes, of course. That's what you programmed. If you didn't like that, why
did you write it like this?

> SomeClass::length = 27;
> SomeClass* p = new SomeClass [65536];

Which effectively gives you 65536 arrays of 27 arrays of doubles.

> But now suppose I want SEVERAL arrays, and 'length' may be different from
> array to array.

Use std::vector< std::vector< double > > ?
That's what the standards committee will respond to this example, I think.
But what arrays are you comparing?

> The easy way is to change SomeClass

>  class SomeClass2 {
>  private:

>      int length_private;        <<<<<<<<<<<<<<<
>      double* array;
>      static int length;

>      SomeClass2()
> {
>          length_private = length;    <<<<<<<<<<<
>          array = new double [ length ];
>      }
>  };

> So I can use:

>  SomeClass2::length = 27;
> SomeClass2* p = new SomeClass2 [65536];
>  SomeClass2::length = 25;
> SomeClass2* q = new SomeClass2 [65536];

Which makes sense, somehow. Apparently in this design you'll need to store per
SomeClass object how big the array is. I.e. you have 65536*25 and 65536*27 doubles
But every one of the 65536 arrays of double in *p is 27 doubles long.

> But SomeClass2 is bigger than SomeClass, because each object contains a copy of the length.

If you don't like that this is contained as an object member, why not make it a type
parameter instead?:

template < int length >
class SomeClassT {
 double array[length];
}


SomeClassT<25>* p = new SomeClassT<25>[65536];

This will store the size information embedded in the type. And the compiler
 can handle everything at compile time.

> Suppose, instead, I declare an environment (which formally is an object!) and I activate it:
> environment<SomeClass> env_1, env_2;
> using environment env_1;
> SomeClass::length = 17;
> using environment env_2;
> SomeClass::length = 24;

> This is similar to namespace syntax, but since "environment" is an object, the following code is valid
> environment<SomeClass> env[14];
>  for (int i=0;i<14;i++)
>  {
>      using environment env[i];
>     // do_something
>  }

Unfortunately, this can't do what you think it does. Each SomeClass will have to store a pointer
to its environment object, which means it loses the size advantage.
Furthermore, you don't need a keyword for this. Traits do quite the same thing already
without new keywords.

I.e.
template <typename SomeClass>
class SomeClassTraits { const int length = 25; // default };
template <>
class SomeClassTraits<SomeClass27> { const int length = 27; }

I have a strong feeling you don't have a clear design, and new keywords won't fix that
for you. I suggest you first make a clear design. Implement that without worrying over
memory overhead (it's only 4 bytes per 25 doubles worst case ? ). If performance is
not good enough and you finished you code before the deadline, find the cause. If it
happens to be because of the xtra 4 bytes, come back & we'll explain how templates can
solve any remaining problem But I don't think even that won't be needed.

--
Michiel Salters
Michiel.Salters@cmg.nl
salters@lucent.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Barry Margolin <barmar@genuity.net>
Date: Tue, 13 Feb 2001 15:30:26 GMT
Raw View
In article <96b13r$2mie$1@stargate1.inet.it>,
Mycroft Holmes <holmes@technologist.com> wrote:
>But SomeClass2 is bigger than SomeClass, because each object contains a
>copy of the length.
>Suppose, instead, I declare an environment (which formally is an object!)
>and I activate it:
>
>
>environment<SomeClass> env_1, env_2;
>using environment env_1;
>SomeClass::length = 17;
>using environment env_2;
>SomeClass::length = 24;
>
>This is similar to namespace syntax, but since "environment" is an
>object, the following code is valid

This solution would also make the new SomeClass bigger, possibly bigger
than SomeClass2.  Instead of just adding an int to each SomeClass object,
each object would have a pointer to the environment object.  I guess if the
class has lots of members like this, and there are several objects in each
environment, you could get some savings.  But is the programming style that
this addresses really so common that this mechanism is needed?  You could
always just use a class of your own:

class length_environment {
 public:
  int length;
  ...
};

class someClass {
  length_environment *le;
  double *array;
 public:
  static length_environment *default_length_environment;
  someClass(length_environment *env = default_length_environment) {
    le = env;
    array = new double[le->length];
};

--
Barry Margolin, barmar@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]