Topic: using inside a class
Author: do-not-spam-ben.hutchings@businesswebsoftware.com (Ben Hutchings)
Date: Fri, 11 Apr 2003 18:49:02 +0000 (UTC) Raw View
In article <EVzka.6$Zt6.1833@news.ecrc.de>, "cody" wrote:
> some of my c++ knowledge got lost during my java lessons so forgive me if
> iam wrong but i think you can do this:
>
> namespace std
> {
> class MyClass
<snip>
No, you mustn't add names to namespace std, just as you wouldn't create
packages under the java package. The whole point of the namespace is
that it's reserved for the standard library. You can specialise
templates defined in the std namespace, but that's not the same.
(See standard section 17.4.3.1 paragraph 1.)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dsp@bdal.de (Daniel Spangenberg)
Date: Fri, 11 Apr 2003 18:49:09 +0000 (UTC) Raw View
cody schrieb:
> some of my c++ knowledge got lost during my java lessons so forgive me if
> iam wrong but i think you can do this:
>
> namespace std
> {
> class MyClass
> {
> string getText() const;
> void setText(string text);
> };
> }
>
Although possible from the standpoint of the language, this is a very bad
idea.
According to 17.4.3.1/p. 1 it will lead to undefined behaviour:
"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 userdefined name of external
linkage and
unless the specialization meets the standard library requirements for the
original template."
The example you give is not a valid exception from this rule.
> anyway, a using clause into a header doesn't make a namespace global, only
> global for the files where this header is included.
Strictly spoke, this is correct, but is also nearly as bad as declaring it in
the global
namespace, because header files are interface files and might be included
anywhere,
especially in OTHER header files....
> putting the using clause into the cpp file where the implementation of the
> class is can also be a good idea.
This is an acceptable way (except of selective using declarations) to migrate
to namespaces.
Greetings from Bremen,
Daniel Spangenberg
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: neilgroves_no_spam@dial.pipex.com ("Neil Groves")
Date: Fri, 11 Apr 2003 18:49:34 +0000 (UTC) Raw View
Some very good points cody.
The trouble with the code you have supplied is that MyClass is inside the
std namespace. I wish to use std not extend it.
I think I mentioned that using namespace std put the namespace into global
scope, indeed you are of course correct that this only occurs when including
the header file with the using namespace statement. However this is an
unnacceptable side-effect of including a header file IMHO.
I definately agree that the using namespace statement is all fine in a cpp
file. This is one place where it all works beautfully. The best approach I
have seen thus far is to typedef the items being used from the namespace.
This has the limitations that due to the lack of typedef templates we can
not leave template parameters open, and that new types are being defined
which must be kept in sync between the header and the implementatin file.
The synchronisation issue is only minor I suppose.
I still think, all things considered, that adding the ability to state that
using within a class would be a useful addition to the language.
Neil Groves
""cody"" <deutronium@web.de> wrote in message
news:EVzka.6$Zt6.1833@news.ecrc.de...
> some of my c++ knowledge got lost during my java lessons so forgive me if
> iam wrong but i think you can do this:
>
> namespace std
> {
> class MyClass
> {
> string getText() const;
> void setText(string text);
> };
> }
>
> anyway, a using clause into a header doesn't make a namespace global, only
> global for the files where this header is included.
>
> putting the using clause into the cpp file where the implementation of the
> class is can also be a good idea.
>
> --
> cody
>
> Freeware Tools, Games and Humour
> http://www.deutronium.de.vu
> [noncommercial and no fucking ads]
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: llewelly.at@xmission.dot.com (LLeweLLyn)
Date: Fri, 11 Apr 2003 18:50:15 +0000 (UTC) Raw View
deutronium@web.de ("cody") writes:
> some of my c++ knowledge got lost during my java lessons so forgive me if
> iam wrong but i think you can do this:
>
> namespace std
If you replace 'std' with some other namespace, you are correct - but
std is reserved to the implementation, and ordinary users are not
allowed add anything to it.
> {
> class MyClass
> {
> string getText() const;
> void setText(string text);
> };
> }
>
> anyway, a using clause into a header doesn't make a namespace global, only
> global for the files where this header is included.
>
> putting the using clause into the cpp file where the implementation of the
> class is can also be a good idea.
[snip]
I think this is usually the best compromise.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: llewelly.at@xmission.dot.com (LLeweLLyn)
Date: Tue, 8 Apr 2003 16:18:59 +0000 (UTC) Raw View
neilgroves_no_spam@dial.pipex.com ("Neil Groves") writes:
> Hi everyone!
>
> First let me apologise for any idiocy in my posting; this is an inherent
> problem due to being an idiot. However, I was thinking that it would be
> quite nice to be able to avoid typing std::string for example all over the
> header files of my C++ classes. Now we all know that placing using
> namespace std; inside a header file is frowned upon quite rightly for
> bringing the std namespace into global scope. Would it be possible in the
> existing ISO language to be able to do something like:
>
> class MyClass
> {
> using namespace std;
>
> string getText() const;
> void setText(string text);
> };
#include<string>
namespace mine
{
using std::string;
string foo(string);
}
is ISO C++ to the best of my knowledge.
It has the disadvantage that 'using namespace mine' will bring in the
name 'string', something the user may not expect.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dan.r.mcleran@seagate.com (Dan McLeran)
Date: Tue, 8 Apr 2003 18:11:18 +0000 (UTC) Raw View
>Would it be possible in the
> existing ISO language to be able to do something like:
>
> class MyClass
> {
> using namespace std;
>
> string getText() const;
> void setText(string text);
> };
>
> I know this does not compiler on any current C++ compiler I have. Is it
> inherently a stupid idea?
I never thought to try this before. I find it a little strange that
this does not work. But, sadly, I too am an idiot. An easy work-around
is something like this:
#include <iostream>
#include <string>
#include <stdlib.h>
class Test
{
//using namespace std;//does not compile
typedef std::string mystring;//typedef once to avoid copious std::
public:
mystring what()
{
mystring s("hi");
return s;
}
};
int main(int argc, char *argv[])
{
using namespace std;
Test t;
cout<<t.what()<<endl;
system("PAUSE");
return 0;
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Wed, 9 Apr 2003 02:53:24 +0000 (UTC) Raw View
"Dan McLeran" <dan.r.mcleran@seagate.com> wrote in message
news:19b0e504.0304080802.240afd8@posting.google.com...
[snip]
> #include <iostream>
> #include <string>
> #include <stdlib.h>
>
> class Test
> {
> //using namespace std;//does not compile
> typedef std::string mystring;//typedef once to avoid copious std::
>
[snip]
Many people use:
typedef std::string string;
which has roughly the same effect as the (invalid) 'using std::string'.
Roger Orr
--
MVP in C++ at www.brainbench.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: neilgroves_no_spam@dial.pipex.com ("Neil Groves")
Date: Thu, 10 Apr 2003 05:50:39 +0000 (UTC) Raw View
Thanks chaps,
I do use the typedef approach frequently as it seems to be the best
available work around. My dislikes for the typedef approach are quite
simple:
1. I am introducing new typenames when I really just want to use typenames.
2. I cannot leave template parameters open. I can not do typedef
boost::shared_ptr shared_ptr; without template parameters because of the
lack of template typedefs.
I like the typedef approach because it avoids having to change all the std::
to something else if I change library completely. Although for the STL
example that is extremely unlikely.
Given that you clever people are also working around the issue, is it a
worthwhile and simple language addition to allow 'using' to work with class
scope? It seems to me that it would result in code that contained less
clutter than with either the typedef's or the fully qualify names. If, in
the end developers need to fully qualify names at almost all of their uses,
I put it to the C++ community that they are virtually pointless as one could
have just used longer typenames without the namespace to similar (although
not identical) effect. Namespaces as they currently stand seem to be a huge
convenience for the library implementers (as they don't need to type the
longer names), but fail to make it as easy as possible for the library
users. I believe, humbly, that namespaces should make it easy to use
symbols from multiple libraries, with very little extra effort in the usual
case where there are not any collisions, and provide capabilities to handle
collisions effectively. Perhaps, and this is just a suggestion based on
very little knowledge, a look at the Eiffel lanuage handling of Multiple
Inheritence might provide some inspiration as to handling the name
collisions that can occur.
These are my views, but I am humble enough to accept a good technical
argument to the contrary. I'm hoping my ideas might help someone somewhere.
Maybe someone out there already as a complete solution in ISO C++, I really
am hoping this is the case.
Thanks everyone for your time,
Neil Groves
""Roger Orr"" <rogero@howzatt.demon.co.uk> wrote in message
news:b6vifn$d9c$1$8300dec7@news.demon.co.uk...
> "Dan McLeran" <dan.r.mcleran@seagate.com> wrote in message
> news:19b0e504.0304080802.240afd8@posting.google.com...
> [snip]
> > #include <iostream>
> > #include <string>
> > #include <stdlib.h>
> >
> > class Test
> > {
> > //using namespace std;//does not compile
> > typedef std::string mystring;//typedef once to avoid copious std::
> >
> [snip]
>
> Many people use:
>
> typedef std::string string;
>
> which has roughly the same effect as the (invalid) 'using std::string'.
>
> Roger Orr
> --
> MVP in C++ at www.brainbench.com
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: deutronium@web.de ("cody")
Date: Fri, 11 Apr 2003 07:18:49 +0000 (UTC) Raw View
some of my c++ knowledge got lost during my java lessons so forgive me if
iam wrong but i think you can do this:
namespace std
{
class MyClass
{
string getText() const;
void setText(string text);
};
}
anyway, a using clause into a header doesn't make a namespace global, only
global for the files where this header is included.
putting the using clause into the cpp file where the implementation of the
class is can also be a good idea.
--
cody
Freeware Tools, Games and Humour
http://www.deutronium.de.vu
[noncommercial and no fucking ads]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: neilgroves_no_spam@dial.pipex.com ("Neil Groves")
Date: Sat, 5 Apr 2003 02:20:51 +0000 (UTC) Raw View
Hi everyone!
First let me apologise for any idiocy in my posting; this is an inherent
problem due to being an idiot. However, I was thinking that it would be
quite nice to be able to avoid typing std::string for example all over the
header files of my C++ classes. Now we all know that placing using
namespace std; inside a header file is frowned upon quite rightly for
bringing the std namespace into global scope. Would it be possible in the
existing ISO language to be able to do something like:
class MyClass
{
using namespace std;
string getText() const;
void setText(string text);
};
I know this does not compiler on any current C++ compiler I have. Is it
inherently a stupid idea?
Regards,
Neil Groves
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]