restrictions of std namespace

J

Jess

Hello,

I learned that we are not allowed to add any new templates or classes
or functions or anything else to std namespace. Does it mean that
"std" can never be extended? I can't even add any simple function or
class?

Thanks,
Jess
 
V

Victor Bazarov

Jess said:
Hello,

I learned that we are not allowed to add any new templates or classes
or functions or anything else to std namespace.

The rule is less strict. We're allowed to add any specialisations of
templates from 'std' _to_ 'std'.
Does it mean that
"std" can never be extended? I can't even add any simple function or
class?

Nothing simple. Only specialisations of standard templates.

V
 
J

Jess

The rule is less strict. We're allowed to add any specialisations of
templates from 'std' _to_ 'std'.


Nothing simple. Only specialisations of standard templates.

V


Thanks! Also, does the specialized version have to be in the same
namespace as std? is it legal if i use another namespace or use global
namespace?

Thanks,
Jess
 
V

Victor Bazarov

Jess said:
Thanks! Also, does the specialized version have to be in the same
namespace as std?

Uh... "in the same namespace as std"? By "std" do you just mean "the
standard item it specialises"?
is it legal if i use another namespace or use global
namespace?

Yes, it's legal, as long as your compiler can find it when needed.

V
 
A

Amol

I would like to ask two simple questions.

If you extend the std namespace how will you port your code to other
platform?

If you make changes to the std namespace how can others will port
their code to your machine?

Remember it's a **standard** and you are not a member of the standards
committee if you want to extend the std namespace try to become member
of the standards committee. Hope you will become that one day.
 
V

Victor Bazarov

Amol said:
I would like to ask two simple questions.

If you extend the std namespace how will you port your code to other
platform?

Not sure what problem you are thinking of. Extending 'std' namespace
and portability are orthogonal. Your program will contain the 'std'
namespace extension, and since the contents of 'std' namespace are
essentially the same (the common denominator, anyway) on all platforms,
you just bring your extension to the target platform, and it should
compile without a problem.
If you make changes to the std namespace how can others will port
their code to your machine?

Again, if others have written portable programs, how would an extension
of 'std' namespace in _only one_ of your programs affect that?
Remember it's a **standard** and you are not a member of the standards
committee if you want to extend the std namespace try to become member
of the standards committee. Hope you will become that one day.

WTF are you talking about?

If I need to implement a specialisation of 'std::less' template for
my particular type (for whatever reason), I am _explicitly allowed_ to
do so by the Standard Committee. But the extend of my extension is
limited to my own code. I can share it with you, and then it becomes
part of your own code, and none of that affects anybody else who does
not add those extensions to their programs.

V
 
J

Jim Langston

Jess said:
Hello,

I learned that we are not allowed to add any new templates or classes
or functions or anything else to std namespace. Does it mean that
"std" can never be extended? I can't even add any simple function or
class?

I think the reasoning behind this is that if you add some new template/class
to namespace std, then a new template/class gets added to the std namespace
in the next version that happens to have a name conflict with yours, your
code breaks. Which is what namespaces are trying to prevent in the first
place, name collisions.

I think the restriction is on adding new names into the std namespace that
may cause collisions. According to V extending templates and such is
allowed, but then you're not adding a *new* name are you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,498
Members
48,185
Latest member
abhaysingh01

Latest Threads

Top