Re: "using namespace" within a class declaration?

From: John Carson (donaldquixote_at_datafast.net.au)
Date: 04/29/04


Date: Thu, 29 Apr 2004 15:56:52 +1000


"Jacek Dziedzic" <jacek__NOSPAM__@janowo.net> wrote in message
news:c6p955$hje$3@korweta.task.gda.pl
> Is it valid to use a "using namespace foo" (as opposed to
> using foo::bar which I'm sure is legal) within a class
> declaration? My compiler rejects it, but I've been told it's
> valid.
>
> Can anyone please confirm or deny?
>
> TIA,
> - J.

Both Comeau online and VC++ 7.1 reject both and say that using declarations
must involve base class names. For example, the following won't compile:

#include <iostream>
class A
{
public:
    using std::cout;
    A()
    {
        cout << "A constructed\n";
    }
};

nor will

#include <iostream>
class A
{
public:
    using namespace std;
    A()
    {
        cout << "A constructed\n";
    }
};

On the other hand, if you move the using directive/declaration inside a
function, it is a different story. The following is fine:

class A
{
public:
    A()
    {
        using namespace std;
        using std::cout;
        cout << "A constructed\n";
    }
};

-- 
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


Relevant Pages

  • Re: Question about multiple files
    ... No. extern just means: The actual definition is somewhere else. ... the compiler can differentiate between declaration and definition just ... > declaration it finds and in this way can I write code expecting it to be ... When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question about multiple files
    ... but say I declare with extern. ... > the compiler can differentiate between declaration and definition just ... >> declaration it finds and in this way can I write code expecting it to be ... > When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Cpp Considered Harmful
    ... >> programming language, the compiler, the IDE, the libraries, etc. ... source file, the implementation shall locate the declaration, (and ... to name one of the defining source files on the command line that initiates ... The counterpart to this in Java is accomplished using the following: ...
    (comp.lang.cpp)
  • Re: Program Fails When Parameter Fixed Constants are Changed (F77) ??
    ... ZBRENT is a function as opposed to a subroutine. ... I don't know how to tell the compiler DCPVAL is a fun and not a sub. ... That is not the same thing as being valid f77. ... declared the name of the External as real throughout, one declaration ...
    (comp.lang.fortran)
  • Re: Internal compiler error C1001 after SP6 installation
    ... Declaring variables like ... fixed it is not particularly productive to worry about why the compiler is choking. ... >The stdafx.h contains a class declaration at line 30. ... the optimizations should caused these error. ...
    (microsoft.public.vc.mfc)