Topic: Namespace extension


Author: boukanov@hadron.fi.uib.no (Igor Boukanov)
Date: 1996/02/21
Raw View
Pulkkinen Esa (esap@cs.tut.fi) wrote:
> This raises immediately the following questions:

> template <class T> namespace { // an unnamed namespace template - allowed?
>   int i; // how many copies of this will be in the program?
>                 // what's the syntax for accessing the variable?
>                 // i<T> ?  <T>::i ?
>   typedef vector<T>::iterator vector_iterator;  // is this the same
>                                                 // as a typedef template?
> }

  From the general point of view such lines would not be allowed becouse
they can be written as
template <class T> namespace unique {
 ...
}
using namespace unique<T>;

but namespace unique is predefined and template names can not be
overloaded.


> template <class T> namespace X { int f(); }

> template <class T> using X<T>::f(); // Allowed?
> template <class T> using namespace X<T>; // Allowed?

> [points to justify namespace templates removed]

I agree that such lines should not be allowed. But this would not be another
rule about template namespace, I propose to extend the template to allow
template namespace declarations, NOT a template "using" declarations.
And this does not prohibit next lines:

template<class T> namespace A {
   void f(T);
   ...
}

template<class T> namespace B {
   using A<T>;
   ...
   void g(T t) { return f(t); } // call A<T>::f
}

> >  3. It makes C++  easy to study, because somebody will have to know only
> >the general idea about templates and how to apply them to any C++ elements
> >(function, class, namespase,...).

> This would only be true if also typedef templates and union templates
> were introduced too.
> --

I agree that reason 3 should be removed. But here is another reason:
   Non-template class with only public static members can be replaced by
namespace declaration. But why template class with only public static
members could not be replaced by corresponding template namespace?

--
Regards, Igor Boukanov (igor.boukanov@fi.uib.no).
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/02/21
Raw View
>Pulkkinen Esa (esap@cs.tut.fi) wrote:
>> >  3. It makes C++  easy to study, because somebody will have to know only
>> >the general idea about templates and how to apply them to any C++ elements
>> >(function, class, namespase,...).
>
>> This would only be true if also typedef templates and union templates
>> were introduced too.

C++ already has union templates.

According to the draft standard, unions are a special sort of class,
and since we have class templates, we therefore have union templates.
(Encouragingly, the three compilers I tried all supported template unions.)

--
Fergus Henderson              WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au               PGP: finger fjh@128.250.37.3
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/02/22
Raw View
In article cdt@ugress.uib.no, boukanov@hadron.fi.uib.no (Igor Boukanov)
writes:

>template <class T> namespace unique {
> ...
>}
>using namespace unique<T>;
>
>but namespace unique is predefined ...

Actually not. The description of the unnamed namespace uses the word
"unique" in italics to represent an arbitrary name chosen by the
compiler which is different from all other names in the program.

You can use the name "unique" for any purpose; it is not reserved or
predefined.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]





Author: boukanov@kvark.fi.uib.no (Igor Boukanov)
Date: 1996/02/16
Raw View
   Of cause, probably my proposal is of time, but namespaces are quite
new C++ feature, and only now I realize how to make it more flexible...

So I suggest to let template namespaces. In this case it will be possible
to write something like this:

// declaration
template<class T> namespace containers {
   class simple_vector {
      private:
         T* data;
         unsigned count;
      public:
         simple_vector(unsigned acount);
         T& operator[](unsigned i);
         .. // other methods
   };

   class simple_list{
      ... // data members and methods
   };
}

...

//implementations
template<class T> namespace containers {

   simple_vector::simple_vector(unsigned acount) {
      count = acount;
      data = new T[acount];
   }

   T* simple_vector::operator[](unsigned i) { return data[i]; }

   ... // methods for simple_list;
}

And now how to use it:

void example_of_using_1(){
   containers<int>::simple_vector v(10);
   containers<float>::simple_list v(10);
};


void example_of_using_2(){
   using namespace containers<char>;
   simple_vector char_vector(100);
};


I think there are at least next reasons to introduce this extension.

  1. It can make declarations and implementations of template libraries much
more clear because one does not need to write template keyword and something
like <T> everywhere. (Just look at the example: I used "template" word only
twice and I never used <T> inside containers namespace.)

  2. It will be much more easy to instant such library for particular
template parameters.
(For a example, containers from example can be instanted by single line:
template namespace containers<bool>;
instead of such line for each template class.)

  3. It makes C++  easy to study, because somebody will have to know only
the general idea about templates and how to apply them to any C++ elements
(function, class, namespase,...).

   So, what do you think about this?

--
Regards, Igor Boukanov (igor.boukanov@fi.uib.no).
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]





Author: esap@cs.tut.fi (Pulkkinen Esa)
Date: 1996/02/20
Raw View
In article <4g2aj2$bko@ugress.uib.no>,
Igor Boukanov <boukanov@kvark.fi.uib.no> wrote:
>So I suggest to let template namespaces. In this case it will be possible
>to write something like this:
[Examples of namespace templates removed]

This raises immediately the following questions:

template <class T> namespace { // an unnamed namespace template - allowed?
  int i; // how many copies of this will be in the program?
                // what's the syntax for accessing the variable?
                // i<T> ?  <T>::i ?
  typedef vector<T>::iterator vector_iterator;  // is this the same
                                                // as a typedef template?
}
template <class T> namespace X { int f(); }

template <class T> using X<T>::f(); // Allowed?
template <class T> using namespace X<T>; // Allowed?

[points to justify namespace templates removed]
>  3. It makes C++  easy to study, because somebody will have to know only
>the general idea about templates and how to apply them to any C++ elements
>(function, class, namespase,...).

This would only be true if also typedef templates and union templates
were introduced too.
--
   Esa Pulkkinen                        | C++ programmers do it virtually
   E-Mail:  esap@cs.tut.fi              | everywhere with a class, resulting
   WWW   :  http://www.cs.tut.fi/~esap/ | in multiple inheritance.
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std.c++-request@ncar.ucar.edu
]