473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

WG21 paper on templated namespaces

Hi all,

I have been searching through the list of papers at:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/

I seem to remember a proposal regarding templated namespaces e.g:

template <typename T>
namespace X{

}

Unfortunately I cant find it. Does anyone know the name/number of the
paper I'm thinking of or have I just invented it?

regards
Andy Little

Jun 15 '06 #1
15 1777
kwikius wrote:
[..]
I seem to remember a proposal regarding templated namespaces e.g:

template <typename T>
namespace X{

}
[..]


What is it supposed to do?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 15 '06 #2
Victor Bazarov wrote:
kwikius wrote:
[..]
I seem to remember a proposal regarding templated namespaces e.g:

template <typename T>
namespace X{

}
[..]


What is it supposed to do?


I sometimes use a struct for grouping stuff to help in source code
legibility and to save typing:

template <typename T>
struct length{
typedef something_ugly<T,...> mm;
typedef somethingelse_ugly<T,...> m;
};

// declare a something_ugly
length<double>::mm mylength;

The problem with a struct is you cant reopen it, which would be a
desirable feature. Maybe I want to add inches:

template<typename T>
namespace length{
typedef something_else_again<T,...> in;
}

length<double>::in mylength1;

I thought I had seen i a paper about this, but cant find it now. Does
anyone know what I'm talking about? (hmm do I really want these guys to
answer that? ... ;-) )

regards
Andy Little

Jun 15 '06 #3
kwikius wrote:
Victor Bazarov wrote:
kwikius wrote:
[..]
I seem to remember a proposal regarding templated namespaces e.g:

template <typename T>
namespace X{

}
[..]
What is it supposed to do?


I sometimes use a struct for grouping stuff to help in source code
legibility and to save typing:

template <typename T>
struct length{
typedef something_ugly<T,...> mm;
typedef somethingelse_ugly<T,...> m;
};

// declare a something_ugly
length<double>::mm mylength;

The problem with a struct is you cant reopen it, which would be a
desirable feature. Maybe I want to add inches:

template<typename T>
namespace length{
typedef something_else_again<T,...> in;
}

length<double>::in mylength1;


Now, tell me, why can't you add inches to the original struct?

Namespaces have a certain purpose. Mostly it's to prevent name collisions
and not save typing or grouping for legibility's sake.
I thought I had seen i a paper about this, but cant find it now. Does
anyone know what I'm talking about? (hmm do I really want these guys
to answer that? ... ;-) )


Try asking in 'comp.std.c++', though.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 15 '06 #4

Victor Bazarov wrote:
kwikius wrote:
Victor Bazarov wrote:
kwikius wrote:
Now, tell me, why can't you add inches to the original struct?
Its a library. I can't know all the units a user might want to make use
of.
Namespaces have a certain purpose. Mostly it's to prevent name collisions
and not save typing or grouping for legibility's sake.
Sure thats your moral position. OTOH I find it convenient to use a
struct for grouping and save typing. Member typedefs are a well known
idiom for traits classes for example.

A namespace is reopenable which is a useful feature it has over a
struct.
Try asking in 'comp.std.c++', though.


Yes. I was hoping someone might give me an instant answer though!

regards
Andy Little

Jun 15 '06 #5
kwikius wrote:
[..]
A namespace is reopenable which is a useful feature it has over a
struct.


You can always *derive* from a struct, can't you?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 15 '06 #6
Victor Bazarov wrote:
kwikius wrote:
[..]
A namespace is reopenable which is a useful feature it has over a
struct.


You can always *derive* from a struct, can't you?


Yes. The current solution seems to be to derive the library struct (
'length' above) from a (by default empty) struct allocated to the user,
but a templated namespace would be superior I think.

FWIW its a major idiom in my physical quantities library pqs, which
should help explain why I want to do it this particular way for those
interested.

http://tinyurl.com/7m5l8

regards
Andy Little

Jun 15 '06 #7

kwikius wrote:
Victor Bazarov wrote:
kwikius wrote:
Victor Bazarov wrote:
> kwikius wrote:
Now, tell me, why can't you add inches to the original struct?


Its a library. I can't know all the units a user might want to make use
of.
Namespaces have a certain purpose. Mostly it's to prevent name collisions
and not save typing or grouping for legibility's sake.


Sure thats your moral position. OTOH I find it convenient to use a
struct for grouping and save typing. Member typedefs are a well known
idiom for traits classes for example.

A namespace is reopenable which is a useful feature it has over a
struct.


So given that it's reopenable, how would you instantiate a namespace
template? Piece by piece, I suppose: instantiate those parts that are
visible.

I could see it being a shorthand that could save a lot of repetitive
template syntax. E.g.

template <typename T> namespace N {
int foo(const T &);
}

Here, uses of the template ID N::foo without template arguments would
deduce T.

Would this be allowed?

template <typename T> namespace N {
void foo(); // no arguments
}

Here, nothing in the foo type signature depends on T. But foo could
refer to T things in the namespace.

template <typename T> namespace N {
T t;
void reset() { T = 0; }
}

I suppose it's similar to a class with a static member and static
function:

template <typename T> class C {
public:
static T t;
static void reset() { t = 0; }
};

int C<int>::t;

int main()
{
C<int>::reset();
return 0;
}

Basically, here we are just using the class as a namespace, right? It
has only static members. We never instantiate the class. Instantiation
of the template is forced by the definition of the static member.

You have to repeat that definition for every type.

The template namespace could do away with that inconvenience, since the
namespace could enclose a definition. But then that definition would
have to be in scope in order to be implicitly instantiated.

Classes are different from namespaces because they separate the
declaration from the implementation. The entire declaration is visible
at once. Where it's not visible, the class is not known. A namespace is
not necessarily all visible at once. Moreover, if a function has a body
in a class declaration, it's an inline function, which is not true of
namespaces. A namespace can contain noninline functions.

So there would have to be some special rules for namespace template
instantiation, such as: 1) instantiate just the necessary parts, from
whatever part of the templated namespace that is visible, and place the
instantiations into the translation unit where the reference is made.
Full function and object definitions are instantiated automatically if
implicit instantiation takes place. If only declarations are visible,
they are instantiated, but the definitions are not.

E.g.
// File1.cc

template <type T> namespace N {
int def_obj = 0;

extern int decl_obj;

int def_fun()
{
return def_obj + decl_obj;
}

int decl_fun();

int foo;
}

// ...

N::def_fun(); // error, ambiguous

N<int>::def_fun();
// requests N<int>::def_fun() instantiation.
// also requests N<int>::def_obj instantiation, needed by the body.
// also requests N<int>::decl_obj instantiation, but that is just a
decl
// so this won't link unless N<int>::decl_obj is generated somewhere.

N<int>::decl_fun();
// "instantiates" the declaration, but a definition must be
// made somewhere.

Now suppose there is a second file:

// File2.cc

template <typename T> namespace N {
int decl_obj = 42;
}

// alternate syntax; must use template arguments on N

template <typename T>
int N<T>::decl_fun()
{
return decl_obj;
}

// now we must force instantiation somehow.

// How about explicit request?
// Generates over all /visible/ N<T>.
// E.g. does not generate N<int>::foo since
// N<T>::foo is declared in the other translation unit,
// which is not known here.

template namespace N<int>;

Try asking in 'comp.std.c++', though.


Yes. I was hoping someone might give me an instant answer though!

regards
Andy Little


Jun 15 '06 #8
Kaz Kylheku posted:

I could see it being a shorthand that could save a lot of repetitive
template syntax. E.g.

template <typename T> namespace N {
int foo(const T &);
}


I like this! We could then have code like the following:

template<class T> namespace N {

int Compare( const T *, const T * )
{
// ...
}

T* Concantenate( const T *, const T * )
{
// ...
}

}
void ArbitraryFunc()
{
using namespace N<wchar_t>;

const wchar_t *p1 = L"First";
const wchar_t *p2 = L"Second";

Compare( p1, p2 );

Concantenate( p1, p2 );
}


--

Frederick Gotham
Jun 15 '06 #9

Hi Kaz,

FWIW I posted your post separately to comp.std.c++ (rather than cross
posting)

regards
Andy Little

Kaz Kylheku wrote:
kwikius wrote:
Victor Bazarov wrote:
kwikius wrote:
> Victor Bazarov wrote:
>> kwikius wrote:

Now, tell me, why can't you add inches to the original struct?


Its a library. I can't know all the units a user might want to make use
of.
Namespaces have a certain purpose. Mostly it's to prevent name collisions
and not save typing or grouping for legibility's sake.


Sure thats your moral position. OTOH I find it convenient to use a
struct for grouping and save typing. Member typedefs are a well known
idiom for traits classes for example.

A namespace is reopenable which is a useful feature it has over a
struct.


So given that it's reopenable, how would you instantiate a namespace
template? Piece by piece, I suppose: instantiate those parts that are
visible.

I could see it being a shorthand that could save a lot of repetitive
template syntax. E.g.

template <typename T> namespace N {
int foo(const T &);
}

Here, uses of the template ID N::foo without template arguments would
deduce T.

Would this be allowed?

template <typename T> namespace N {
void foo(); // no arguments
}

Here, nothing in the foo type signature depends on T. But foo could
refer to T things in the namespace.

template <typename T> namespace N {
T t;
void reset() { T = 0; }
}

I suppose it's similar to a class with a static member and static
function:

template <typename T> class C {
public:
static T t;
static void reset() { t = 0; }
};

int C<int>::t;

int main()
{
C<int>::reset();
return 0;
}

Basically, here we are just using the class as a namespace, right? It
has only static members. We never instantiate the class. Instantiation
of the template is forced by the definition of the static member.

You have to repeat that definition for every type.

The template namespace could do away with that inconvenience, since the
namespace could enclose a definition. But then that definition would
have to be in scope in order to be implicitly instantiated.

Classes are different from namespaces because they separate the
declaration from the implementation. The entire declaration is visible
at once. Where it's not visible, the class is not known. A namespace is
not necessarily all visible at once. Moreover, if a function has a body
in a class declaration, it's an inline function, which is not true of
namespaces. A namespace can contain noninline functions.

So there would have to be some special rules for namespace template
instantiation, such as: 1) instantiate just the necessary parts, from
whatever part of the templated namespace that is visible, and place the
instantiations into the translation unit where the reference is made.
Full function and object definitions are instantiated automatically if
implicit instantiation takes place. If only declarations are visible,
they are instantiated, but the definitions are not.

E.g.
// File1.cc

template <type T> namespace N {
int def_obj = 0;

extern int decl_obj;

int def_fun()
{
return def_obj + decl_obj;
}

int decl_fun();

int foo;
}

// ...

N::def_fun(); // error, ambiguous

N<int>::def_fun();
// requests N<int>::def_fun() instantiation.
// also requests N<int>::def_obj instantiation, needed by the body.
// also requests N<int>::decl_obj instantiation, but that is just a
decl
// so this won't link unless N<int>::decl_obj is generated somewhere.

N<int>::decl_fun();
// "instantiates" the declaration, but a definition must be
// made somewhere.

Now suppose there is a second file:

// File2.cc

template <typename T> namespace N {
int decl_obj = 42;
}

// alternate syntax; must use template arguments on N

template <typename T>
int N<T>::decl_fun()
{
return decl_obj;
}

// now we must force instantiation somehow.

// How about explicit request?
// Generates over all /visible/ N<T>.
// E.g. does not generate N<int>::foo since
// N<T>::foo is declared in the other translation unit,
// which is not known here.

template namespace N<int>;

Try asking in 'comp.std.c++', though.


Yes. I was hoping someone might give me an instant answer though!

regards
Andy Little


Jun 15 '06 #10

Victor Bazarov wrote:
kwikius wrote:
[..]
A namespace is reopenable which is a useful feature it has over a
struct.


You can always *derive* from a struct, can't you?


BTW another important property of a namespace is that you cant't
instantiate it. IOW used as a container a struct has inconvenient
connotations requiring extra work to prevent instantions (e.g deriving
from boost::noncopyable which causes its own problems) . IOW its a pain
in th a** using a struct this way.

regards
Andy Little

Jun 15 '06 #11

kwikius wrote:
Hi Kaz,

FWIW I posted your post separately to comp.std.c++ (rather than cross
posting)


At least I thought I had. If nothing appears on comp.std.c++ in the
next day or so, I'll try again.

regards
Andy Little

Jun 16 '06 #12
* kwikius:
kwikius wrote:
Hi Kaz,

FWIW I posted your post separately to comp.std.c++ (rather than cross
posting)


At least I thought I had. If nothing appears on comp.std.c++ in the
next day or so, I'll try again.


The comp.std.c++ robo-moderator software is currently acting up.
Sometimes it acts like a black hole. Sometimes it forwards the message
to two moderators, one of which will approve it, and one of which will
reject it on the grounds that it's very, almost suspiciously similar to
the one approved. And so on. So it's probably no fault of yours if
that message seemingly disappears: just try again.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 16 '06 #13

Alf P. Steinbach wrote:
* kwikius:
kwikius wrote:
Hi Kaz,

FWIW I posted your post separately to comp.std.c++ (rather than cross
posting)


At least I thought I had. If nothing appears on comp.std.c++ in the
next day or so, I'll try again.


The comp.std.c++ robo-moderator software is currently acting up.
Sometimes it acts like a black hole. Sometimes it forwards the message
to two moderators, one of which will approve it, and one of which will
reject it on the grounds that it's very, almost suspiciously similar to
the one approved. And so on. So it's probably no fault of yours if
that message seemingly disappears: just try again.


Ok. Its there!

regards
Andy Little

Jun 16 '06 #14
Hi Frederick,

Frederick Gotham wrote:
I like this! We could then have code like the following:


[snip details]

Its probably worth re-posting your post to comp.std.c++ FWIW, in the
thread with the same title as this one.

Sorry if I'm stating the blindingly obvious!

regards
Andy Little

Jun 16 '06 #15

kwikius wrote:
Hi Frederick,

Frederick Gotham wrote:
I like this! We could then have code like the following:


[snip details]

Its probably worth re-posting your post to comp.std.c++ FWIW, in the
thread with the same title as this one.


There is no point in doing that. You will invariably be told:

``Wonderful! Now all you have to do is produce a working reference
implementation
in at least one compiler, get some users and spend some time gaining
experience
with the use of that feature.''

So it ultimately doesn't matter where in Usenet you want to discuss it,
as long as it's not too wildly off topic wherever that is.

:)

Jun 16 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Agent Mulder | last post by:
Hi group, we all have our favourite 'lacking features' in C++ and as I get it, it is just a matter of pushing, until some exhausted comite accepts your new keyword. How to push? I found...
3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
3
by: mrstephengross | last post by:
Hi folks... I've been sifting through archived postings on this, but haven't quite found an answer yet. I've got a templated stand-alone function: template<typename Tvoid access(const T & t) { ;...
14
by: Dave Rahardja | last post by:
Hi all, Is it possible to prevent _at compile or link time_ the mulitple instantiation of a templated function? In other words, if there exists a function template <typename Tvoid fn(); I...
2
by: mattjgalloway | last post by:
I'm having some problems with a templated member function of a templated class. Unfortunately I can't replicate it with a simple example so I know something odd must be going on!!! Basically it's...
7
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum,...
6
by: Ioannis Papadopoulos | last post by:
Hi, I have the following code: #include <iostream> int foo() { std::cout << __func__ << std::endl; }
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.