std:: namespace moral question

63 views
Skip to first unread message

Goran Bervar

unread,
Apr 2, 2001, 1:05:55 PM4/2/01
to
Hello,

During the years I've made quite a large library of my own "common"
functions, routines, manipulators I use often ( We all know that standard
missed to declare so many obviously needed functions <g>). Many of them are
written in C or in C++ from times before standard. I finally got some time
to re-write them to make my library standard compliant where possible.

A moral question crossed my mind. Many functions "extend" standard library.
Is it moral to extend standard namespace than? For example let's say I have
a function or template to round any value to the nearest value. Should I put
it in a std:: namespace or my own namespace? I somehow feel extending std::
namespace isn't moral as some other programmer using my library may get
wrong assumption that 'round' is part of the standard library. On the other
hand, if my 'round' is implemented so it is fully standard compliant and
therefore fully compatible,why should I put in my own namespace than?

I wonder what do you think about it. Should we extend list of names in std::
namespace in such case or not? If yes, how would you prescribe what may go
to std:: namespace and what not?

Sincerely,

Goran


[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Ron Natalie

unread,
Apr 3, 2001, 10:19:43 AM4/3/01
to

Goran Bervar wrote:

>
> A moral question crossed my mind. Many functions "extend" standard library.
> Is it moral to extend standard namespace than? For example let's say I have
> a function or template to round any value to the nearest value. Should I put
> it in a std:: namespace or my own namespace?

Your own. It is NOT legal to put your own symbols into the "std" namespace.
17.4.3.1 "It is undefined for a C++ program to add declarations or definitions
in the namespace std ..."


> I somehow feel extending std::
> namespace isn't moral as some other programmer using my library may get
> wrong assumption that 'round' is part of the standard library.

Furthermore, he may have his own "round" and would be quite surprised if
"using namespace std" suddenly caused your function to be inserted.

> On the other
> hand, if my 'round' is implemented so it is fully standard compliant and
> therefore fully compatible,why should I put in my own namespace than?

round can't be standard compliant. There is no such function in the standard.
It's something you made up. It is undefined, and completely bogus, for you to
add it in the std namespace.

André Pönitz

unread,
Apr 3, 2001, 10:23:17 AM4/3/01
to
{Please avoid quoting from C++ draft standards, which can be
inaccurate and creates confusion. The final C++ standard has been
complete since late 1997 (approved since 1998) and contains
numerous and significant changes from previous drafts. When
talking about what the standard says, we should always be
speaking about the same standard -- today's standard -- even if
the words happen to coincidentally be the same as in an earlier
draft. -mod/hps}

Goran Bervar <n.e.b....@yahoo.com> wrote:
> I somehow feel extending std::
> namespace isn't moral as some other programmer using my library may get
> wrong assumption that 'round' is part of the standard library.

The December 96 draft mentions:

17.3.3.1 Reserved names [lib.reserved.names]

1 It is undefined for a C++ program to add declarations or definitions

to namespace std or namespaces within namespace std unless otherwise
specified.

I have not seen an exception which would make your case valid.


Andre'


--
André Pönitz ............................................. poe...@htwm.de
C++-Programmierer gesucht ... Naeheres unter http://mathematik.htwm.de/job

Francis Glassborow

unread,
Apr 3, 2001, 10:26:47 AM4/3/01
to
In article <4CXx6.117$9T....@news.siol.net>, Goran Bervar
<n.e.b....@yahoo.com> writes

>I wonder what do you think about it. Should we extend list of names in std::
>namespace in such case or not? If yes, how would you prescribe what may go
>to std:: namespace and what not?

Read the Standard and it will tell you exactly what you may add to
namespace ::std which is very little. So you do not have a problem.


--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)

Boris Pulatov

unread,
Apr 3, 2001, 10:34:59 AM4/3/01
to
Goran Bervar wrote:
>
> Hello,

>
> I wonder what do you think about it. Should we extend list of names in std::
> namespace in such case or not? If yes, how would you prescribe what may go
> to std:: namespace and what not?
>

>From 17.4.3.1/1:


It is undefined for a C++ program to add declarations or definitions to
namespace std or namespaces within namespace std unless otherwise

specified. A program may add template specializations for any standard
library template to namespace std. Such a specialization (complete or
partial) of a standard library template results in undefined behavior
unless the declaration depends on a user-defined name of external
linkage and unless the specialization meets the standard library
requirements for the original template.

HTH,
Boris Pulatov
remove X from my e-mail address

Siemel Naran

unread,
Apr 3, 2001, 7:08:48 PM4/3/01
to
"Goran Bervar" <n.e.b....@yahoo.com> wrote in message news:4CXx6.117

> I wonder what do you think about it. Should we extend list of names in
std::
> namespace in such case or not? If yes, how would you prescribe what may go
> to std:: namespace and what not?

My char(2): Put your own stuff in your own namespace.

Exception: you may have to specialize std::swap for your own types. True,
Koenig lookup may save you from doing this in many cases. If one calls
"swap(myclass1,myclass2)" the compiler will look in the namespace where
myclass1 and myclass2 live, as well as the current namespace for the best
definition of swap. So it could very well find
Myspace::swap(MyClass&,MyClass&). But sometimes, you'll call
"std::swap(myclass1,myclass2)" and it would be good if std::swap were your
efficient optimized version.


--
+++++++++++
Siemel Naran

Michiel Salters

unread,
Apr 3, 2001, 11:45:26 PM4/3/01
to
In article <4CXx6.117$9T....@news.siol.net>, Goran Bervar says...

>Hello,
>During the years I've made quite a large library of my own "common"
>functions, routines, manipulators I use often ( We all know that standard
>missed to declare so many obviously needed functions <g>). Many of them are
>written in C or in C++ from times before standard. I finally got some time
>to re-write them to make my library standard compliant where possible.

The nice thing about most standard C compliant functions is that they are
also C++ standard compliant. But I agree than often some rewriting will
make them more standard C++ compatible & efficient.

>A moral question crossed my mind. Many functions "extend" standard library.
>Is it moral to extend standard namespace than? For example let's say I have
>a function or template to round any value to the nearest value. Should I put
>it in a std:: namespace or my own namespace? I somehow feel extending std::
>namespace isn't moral as some other programmer using my library may get
>wrong assumption that 'round' is part of the standard library. On the other
>hand, if my 'round' is implemented so it is fully standard compliant and
>therefore fully compatible,why should I put in my own namespace than?

Why you should put it in your own namespace ? Because that's the only
legal & moral place. std:: is off-limits for your functions. They're not
standard.

Imagine what would happen if the C++ committee, in its wisdom sees the need
for a round funcion. Without breaking my programs, it could add round()
to the std:: namespace.

I know there is a small space to do "extend" the std:: namespace. But
I think that anybody who can't explain what is allowed should keep std::
as it is. They shoudln't write "namespace std", except when following
"using".

Regards,
Michiel Salters

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel...@cmg.nl

Robert W Hand

unread,
Apr 4, 2001, 4:45:54 AM4/4/01
to
<snip>

> Is it moral to extend standard namespace than? For example let's say I
have
> a function or template to round any value to the nearest value. Should I
put
> it in a std:: namespace or my own namespace? I somehow feel extending
std::
> namespace isn't moral as some other programmer using my library may get
> wrong assumption that 'round' is part of the standard library. On the
other
> hand, if my 'round' is implemented so it is fully standard compliant and
> therefore fully compatible,why should I put in my own namespace than?
<snip>

Dear Goran,

It is undefined behavior to add declarations or definitions to namespace std
except for specializations of templates of the stl. (subclause 17.4.3.1)
But then, the specialization of an stl template must depend on a
user-defined name with external linkage and the specialization, itself,
meets the stl requirements for the original template. So IMHO, morals and
legality coincide here. I would agree that adding your own functions to std
is misleading and "immoral" as well.

Best wishes,

Bob

Nicola Musatti

unread,
Apr 4, 2001, 4:59:10 AM4/4/01
to

Goran Bervar wrote:
[...]


> A moral question crossed my mind. Many functions "extend" standard library.
> Is it moral to extend standard namespace than? For example let's say I have
> a function or template to round any value to the nearest value. Should I put
> it in a std:: namespace or my own namespace? I somehow feel extending std::
> namespace isn't moral as some other programmer using my library may get
> wrong assumption that 'round' is part of the standard library. On the other
> hand, if my 'round' is implemented so it is fully standard compliant and
> therefore fully compatible,why should I put in my own namespace than?

It is not just a moral question: the standard reserves the std namespace
for compiler implementors. The only things a compiler user can put in
the std namespace are specializazions of standard templates on user
defined types.

Best regards,
Nicola Musatti

André Pönitz

unread,
Apr 4, 2001, 12:47:57 PM4/4/01
to
> {Please avoid quoting from C++ draft standards, which can be
> inaccurate and creates confusion.

Was my quoting inaccurate or has it created confusion? I think I was
clearly stating that I refered to the draft.

> The final C++ standard has been complete since late 1997 (approved
> since 1998) and contains numerous and significant changes from previous
> drafts.

I am aware of this.

> When talking about what the standard says, we should always be
> speaking about the same standard -- today's standard -- even if the
> words happen to coincidentally be the same as in an earlier draft.
> -mod/hps}

As far as I know the final standard is available only to people owning
credit cards. Please correct me if I am wrong.

Andre'

--
André Pönitz ............................................. poe...@htwm.de

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]

Steve Clamage

unread,
Apr 5, 2001, 8:02:11 PM4/5/01
to
Goran Bervar wrote:
>
> A moral question crossed my mind. Many functions "extend" standard library.
> Is it moral to extend standard namespace than? For example let's say I have
> a function or template to round any value to the nearest value. Should I put
> it in a std:: namespace or my own namespace?

The C++ standard is quite clear on this point. You are not allowed to
add anything to namespace std except specializations of standard
templates. (A specialization must be placed in the same namespace
as the primary template.) Such a specialization must involve at least
one user-defined type as a template parameter.


--
Steve Clamage, stephen...@sun.com

Michiel Salters

unread,
Apr 5, 2001, 10:17:56 PM4/5/01
to
In article <9acob0$7if$1...@narses.hrz.tu-chemnitz.de>,
=?iso-8859-1?Q?Andr=E9_P=F6nitz?= says...

>As far as I know the final standard is available only to people owning
>credit cards. Please correct me if I am wrong.

>Andre'

AFAIK, the final standard is available from ANSI, DIN, and other
national organizations. While ANSI is very much a Credit Card-only
organization,
I don't know about DIN. Have you contacted them ?
(They might not have a PDF, version though.)

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel...@cmg.nl

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]

Reply all
Reply to author
Forward
0 new messages