doubt in using declaration in c++?

This is a discussion on doubt in using declaration in c++? within the C++ Programming forums, part of the General Programming Boards category; can we use using declaration within name space scope? is it safe to use it? I think we can use ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    162

    Thumbs up doubt in using declaration in c++?

    can we use using declaration within name space scope? is it safe to use it?
    I think we can use using declaration for class scope and function scope only. please correct me if am wrong?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    Show an example of what you mean. But remember you should keep the using declarations as local as possible, and maybe even prefer using the scope resolution operator::.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    162
    Quote Originally Posted by jimblumberg View Post
    Show an example of what you mean. But remember you should keep the using declarations as local as possible, and maybe even prefer using the scope resolution operator::.

    Jim
    Code:
    namespace X {
      int i;
      int j;
      void f;
      void g;
      }
    
    using X::j

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348
    yes, you can do that. it's actually a recommended way to do it, especially for large namespaces like std.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    162
    I guess we can use "using" declaration for global scope, local scope, namespace scope and class scope..if there is any limitations, on this let me know?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    Yes there are limitations on when you can use a using declaration. One of the limitations would be if there is a clash between your function/variable name and a name that is already defined in the current namespace. To eliminate all possibilities of name clashes you would always scope the function/variable using the scope resolution operator::.

    Code:
    std::cout << X::j << std::endl;
    You really should get used to using the scope resolution operator::, this is considered the "preferred" method by many. By blindly using the "using namespace" and "using" directives you are circumventing the reason namespaces were introduced.

    Jim

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    I like namespace aliases:

    namespace foo = My::Super::Long::Name::Space;
    foo::HelloWorld();

    I'm just throwing this out here because if you find it it annoying to type out long namespaces, consider namespaces aliases. Of course, you have to prefer using namespaces in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,213

  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by Elysia View Post
    I like namespace aliases:

    namespace foo = My::Super::Long::Name::Space;
    That said, never do that in a header file, only in .cpp files. If you do it in a header, you effectively force your namespace alias on all other users of that header, which is a bad thing. Obviously ,the same applies to

    Code:
    using namespace xyz;

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,535
    While doing that in a header might not be recommended because it introduces a global symbol, it is infinitely more recommended than using namespace. I might consider a namespace alias to be a necessary evil for certain header files with long namespaces.
    Remember, a namespace does not pollute the global namespace (except for introducing a new namespace, essentially) as much as the using namespace does, so you are relatively safe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple doubt on declaration
    By Saurabh Mehta in forum C Programming
    Replies: 3
    Last Post: 11-20-2012, 02:10 AM
  2. Array declaration doubt
    By finalsayan in forum C Programming
    Replies: 8
    Last Post: 03-29-2010, 09:56 AM
  3. Very Useful, BUT its a Doubt
    By cavestine in forum Linux Programming
    Replies: 1
    Last Post: 10-15-2007, 06:14 AM
  4. doubt
    By chinuchinu in forum C Programming
    Replies: 5
    Last Post: 08-19-2006, 03:36 AM
  5. When in doubt, run away
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-07-2004, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21