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?
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?
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
yes, you can do that. it's actually a recommended way to do it, especially for large namespaces like std.
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?
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::.
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.Code:std::cout << X::j << std::endl;
Jim
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.
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.