Thread View: comp.std.c++
4 messages
4 total messages
Started by Igor Boukanov
Fri, 16 Feb 1996 17:06
Namespace extension
Author: Igor Boukanov
Date: Fri, 16 Feb 1996 17:06
Date: Fri, 16 Feb 1996 17:06
77 lines
3052 bytes
3052 bytes
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. ]
Re: Namespace extension
Author: Pulkkinen Esa
Date: Tue, 20 Feb 1996 18:40
Date: Tue, 20 Feb 1996 18:40
38 lines
2430 bytes
2430 bytes
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
]
Re: Namespace extension
Author: Igor Boukanov
Date: Wed, 21 Feb 1996 13:54
Date: Wed, 21 Feb 1996 13:54
67 lines
3006 bytes
3006 bytes
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.
]
Re: Namespace extension
Author: Fergus Henderson
Date: Wed, 21 Feb 1996 16:11
Date: Wed, 21 Feb 1996 16:11
24 lines
1655 bytes
1655 bytes
>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.
]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads