473,785 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1813
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_u gly<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<typena me 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_u gly<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<typena me 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_ob j instantiation, needed by the body.
// also requests N<int>::decl_ob j instantiation, but that is just a
decl
// so this won't link unless N<int>::decl_ob j is generated somewhere.

N<int>::decl_fu n();
// "instantiat es" 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_ob j instantiation, needed by the body.
// also requests N<int>::decl_ob j instantiation, but that is just a
decl
// so this won't link unless N<int>::decl_ob j is generated somewhere.

N<int>::decl_fu n();
// "instantiat es" 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

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

Similar topics

2
1846
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 the answer on comp.std.c++, but it leaves open a few questions:
3
6577
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? Here's what I'm generally trying to achieve: I'm building (trying to anyway) a serialization library. Here's my design:
1
1870
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 my_template
3
1863
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) { ; } And a class 'Foo': struct Foo { friend void access(const Foo & f);
14
1778
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 want to prevent the user from doing this: int main()
2
1690
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 like this... I have a few classes all templated by one type. Inside one of the classes is a templated member function. When I try to use this templated function in another class, the compiler complains with: error: expected primary-expression...
7
1730
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, short Trank, bool Tcont> TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont& tten );
6
1497
by: Ioannis Papadopoulos | last post by:
Hi, I have the following code: #include <iostream> int foo() { std::cout << __func__ << std::endl; }
2
2938
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
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.