Namespace in class?

Discussion in 'C++' started by Nephi Immortal, Feb 10, 2011.

  1. Why can’t I put “using namespace name” inside class body?
     
    Nephi Immortal, Feb 10, 2011
    #1
    1. Advertising

  2. On 2/10/2011 2:34 PM, Nephi Immortal wrote:
    > Why can’t I put “using namespace name” inside class body?


    Because the Standard prohibits it (in [namespace.udir]). Can you tell
    us what might be the purpose? What problem would you solve with that?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 10, 2011
    #2
    1. Advertising

  3. On Feb 10, 1:43 pm, Victor Bazarov <> wrote:
    > On 2/10/2011 2:34 PM, Nephi Immortal wrote:
    >
    > >    Why can’t I put “using namespace name” inside class body?

    >
    > Because the Standard prohibits it (in [namespace.udir]).  Can you tell
    > us what might be the purpose?  What problem would you solve with that?


    Well…. It is what I expect….

    class name {
    using namespace name2;

    func1();
    func2();
    };

    instead of …

    class name {
    name2::func1();
    name2::func2();
    };

    It is much easier rather than placing using namespace name2 inside
    each member function’s body or global scope. I hate to add namespace
    before member function declaration each time like name2::func1(),
    name2::func2(), …..
     
    Nephi Immortal, Feb 11, 2011
    #3
  4. Nephi Immortal

    Rolf Magnus Guest

    Nephi Immortal wrote:

    > On Feb 10, 1:43 pm, Victor Bazarov <> wrote:
    >> On 2/10/2011 2:34 PM, Nephi Immortal wrote:
    >>
    >> > Why can’t I put “using namespace name†inside class body?

    >>
    >> Because the Standard prohibits it (in [namespace.udir]). Can you tell
    >> us what might be the purpose? What problem would you solve with that?

    >
    > Well…. It is what I expect….
    >
    > class name {
    > using namespace name2;
    >
    > func1();
    > func2();
    > };
    >
    > instead of …
    >
    > class name {
    > name2::func1();
    > name2::func2();
    > };


    Huh? Are those functions suposed to be member functions of the class or non-
    member functions in namespace name2? They can't be both at the same time.
     
    Rolf Magnus, Feb 11, 2011
    #4
  5. Nephi Immortal

    Öö Tiib Guest

    On Feb 11, 5:28 am, Nephi Immortal <> wrote:
    > On Feb 10, 1:43 pm, Victor Bazarov <> wrote:
    >
    > > On 2/10/2011 2:34 PM, Nephi Immortal wrote:

    >
    > > >    Why can’t I put “using namespace name” inside class body?

    >
    > > Because the Standard prohibits it (in [namespace.udir]).  Can you tell
    > > us what might be the purpose?  What problem would you solve with that?

    >
    >         Well….  It is what I expect….
    >
    > class name {
    >         using namespace name2;
    >
    >         func1();
    >         func2();
    >
    > };


    What are these "func1()" and "func2()"? By syntax and location they
    look like constructors but class is name so constructor should be
    "name()"? If these are member function declarations then they lack
    type. C++ does not allow implicit type.


    > instead of …
    >
    > class name {
    >         name2::func1();
    >         name2::func2();
    >
    > };


    That is totally confusing what you try to do here. You can not put
    only part of your class (some members or constructors) into namespace
    with such a syntax. You must put whole class. If you want to put your
    "class name" into "namespace name2" then you should write:

    namespace name2
    {
    class name
    {
    public:
    name();
    int func1();
    };
    }

    >         It is much easier rather than placing using namespace name2 inside
    > each member function’s body or global scope.  I hate to add namespace
    > before member function declaration each time like name2::func1(),
    > name2::func2(), …..


    Yes, but C++ does not let to put namespaces (or even class name)
    before member function name in member function declarations anyway. So
    you seemingly fight with something that is not even allowed?
     
    Öö Tiib, Feb 11, 2011
    #5
  6. On 11/02/2011 03:28, Nephi Immortal wrote:
    > On Feb 10, 1:43 pm, Victor Bazarov<> wrote:
    >> On 2/10/2011 2:34 PM, Nephi Immortal wrote:
    >>
    >>> Why can’t I put “using namespace name” inside class body?

    >>
    >> Because the Standard prohibits it (in [namespace.udir]). Can you tell
    >> us what might be the purpose? What problem would you solve with that?

    >
    > Well…. It is what I expect….
    >
    > class name {
    > using namespace name2;
    >
    > func1();
    > func2();
    > };
    >
    > instead of …
    >
    > class name {
    > name2::func1();
    > name2::func2();
    > };
    >
    > It is much easier rather than placing using namespace name2 inside
    > each member function’s body or global scope. I hate to add namespace
    > before member function declaration each time like name2::func1(),
    > name2::func2(), …..


    You can simulate what you want by doing this:

    ###
    namespace RandomName {

    using namespace NS;

    class C
    {
    //using namespace NS; // illegal
    };

    }

    using RandomName::C;
    ###

    It's actually useful some of the time, too - particularly when you want
    to avoid writing e.g. boost:: a lot in a header (anyone who's familiar
    with Boost.Multi-Index may have been annoyed by this sort of thing). You
    definitely don't want to writing using namespace boost; in the global
    namespace in a header, but you also don't want to write boost:: in front
    of everything. A using directive scoped to the class would be ideal, but
    it's not allowed.

    HTH,
    Stu
     
    Stuart Golodetz, Feb 11, 2011
    #6
  7. On 11/02/2011 20:48, Stuart Golodetz wrote:
    > On 11/02/2011 03:28, Nephi Immortal wrote:
    >> On Feb 10, 1:43 pm, Victor Bazarov<> wrote:
    >>> On 2/10/2011 2:34 PM, Nephi Immortal wrote:
    >>>
    >>>> Why can’t I put “using namespace name” inside class body?
    >>>
    >>> Because the Standard prohibits it (in [namespace.udir]). Can you tell
    >>> us what might be the purpose? What problem would you solve with that?

    >>
    >> Well…. It is what I expect….
    >>
    >> class name {
    >> using namespace name2;
    >>
    >> func1();
    >> func2();
    >> };
    >>
    >> instead of …
    >>
    >> class name {
    >> name2::func1();
    >> name2::func2();
    >> };
    >>
    >> It is much easier rather than placing using namespace name2 inside
    >> each member function’s body or global scope. I hate to add namespace
    >> before member function declaration each time like name2::func1(),
    >> name2::func2(), …..

    >
    > You can simulate what you want by doing this:
    >
    > ###
    > namespace RandomName {
    >
    > using namespace NS;
    >
    > class C
    > {
    > //using namespace NS; // illegal
    > };
    >
    > }
    >
    > using RandomName::C;
    > ###
    >
    > It's actually useful some of the time, too - particularly when you want
    > to avoid writing e.g. boost:: a lot in a header (anyone who's familiar
    > with Boost.Multi-Index may have been annoyed by this sort of thing). You
    > definitely don't want to writing using namespace boost; in the global
    > namespace in a header, but you also don't want to write boost:: in front
    > of everything. A using directive scoped to the class would be ideal, but
    > it's not allowed.
    >
    > HTH,
    > Stu


    As a real example, I have a class template in my most recent project
    that looks like the below. It would be substantially more annoying to
    write the EdgeContainer typedef without the using directive (well, I
    think so anyway).

    Cheers,
    Stu

    ###
    namespace mp {

    namespace mp_AdjacencyGraph {

    using namespace boost::multi_index;

    template <typename NodeProperties, typename EdgeWeight>
    class AdjacencyGraph
    {
    public:
    typedef WeightedEdge<EdgeWeight> Edge;

    private:
    // Tags
    struct tagDefault;
    struct tagSmaller;
    struct tagLarger;

    typedef multi_index_container<
    Edge,
    indexed_by<
    ordered_unique<tag<tagDefault>,
    composite_key<
    Edge,
    member<Edge,int,&Edge::u>,
    member<Edge,int,&Edge::v>
    >
    >,
    ordered_non_unique<tag<tagSmaller>,
    member<Edge,int,&Edge::u>
    >,
    ordered_non_unique<tag<tagLarger>,
    member<Edge,int,&Edge::v>
    >
    >
    > EdgeContainer;

    // etc.
    };

    }

    using mp_AdjacencyGraph::AdjacencyGraph;

    }
    ###
     
    Stuart Golodetz, Feb 11, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    573
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,135
  3. E11
    Replies:
    1
    Views:
    4,784
    Thomas Weidenfeller
    Oct 12, 2005
  4. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
  5. Peng Yu
    Replies:
    0
    Views:
    643
    Peng Yu
    Sep 14, 2008
Loading...

Share This Page